Display Search Result Count

If you are within the search template i.e Search query is your main query. You should then be able to get search results from global $wp_query without running an additional query.

global $wp_query;
echo $wp_query->found_posts.' results found.';

Edit 1

If you have to get count out of search context. You can combine both techniques to get efficient result. It wont fetch all the post but you can get the search count.

$allsearch = new WP_Query("s=$s&showposts=0"); 
echo $allsearch ->found_posts.' results found.';

Your Error

About the error you are getting, it lies here

$allsearch =& new WP_Query("s=$s&showposts=-1");

Remove the “&” beside the equal sign to get rid of the error. So it will look like this

$allsearch = new WP_Query("s=$s&showposts=-1");

Leave a Comment