Family Encyclopedia >> Electronics

How to Display Search Terms and Results Count on Your WordPress Search Page

As experienced WordPress developers, we've helped countless sites enhance their search functionality. In this guide, we'll show you a simple, proven method to display the search query and number of results on your search page—a feature requested by one of our users. Got a topic idea? Email us!

How to Display Search Terms and Results Count on Your WordPress Search Page

Open your active theme's search.php file and add this code snippet just before the main loop (typically before if (have_posts())):

<?php
    global $wp_query;
    if ($wp_query->have_posts()) {
        $search_term = get_search_query();
        $result_count = $wp_query->post_count;
        echo '<h2>' . esc_html($search_term) . ' search results - ' . $result_count . ' items</h2>';
    }
    wp_reset_query();
?>

This outputs clean, user-friendly text like:

twitter search results - 15 items

For better UX, highlight search terms in results by adding this CSS to your theme's stylesheet:

.search-terms {
    background-color: yellow;
    color: blue;
}

This tweak is one of the quickest ways to professionalize your search page. Pro tip: Pair it with result highlighting or category filters for even better WordPress search customization.

Source: Michael Martin