How to: Display Search Term and Result Count – WordPress

Advertisement

In this tutorial, you will learn how to add Search Term and Search Result Count into your WordPress search page, I was looking for this functionality to add to my new WordPress site, currently, the one you’re viewing now yayyyyyy 😉

Search Term and Search Count is a bit helpful for users who search your site, it helps them know how many posts they’re currently viewing.

Open your search.php file under your theme directory and add this code.

Solution 1:


global $wp_query;
$count = $wp_query->post_count;
$term = $wp_query->query_vars; // this result array of available fields.

echo $term['s'].' - '.$count.' Results';

or

Directly call the $GLOBAL[‘wp_query’] array


<?php echo $GLOBALS['wp_query']->post_count; ?>

Solution 2

Though, I didn’t suggest this solution it still works just fine, the reason I didn’t suggest this solution is that it creates new initialzation and that means anothe memory though passed only by referrence.


$allsearch = &new WP_Query("s=$s&showposts=-1");
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count; _e('');
_e('<span class="search-terms">"');  echo $key; _e('"</span>'); _e(' &mdash; ');  echo $count . ' '; _e('results'); 

// Don't forget to reset query
wp_reset_query();

Sample demo / to feel more try searching

SEARCHING FOR PHP – 4 RESULTS

Add CSS code to make your search term and search count alive, that’s it hope it helps ;).

References and Credits

WP Beginner

Advertisement