Posted on

Showing the number of search results

If you want to show the number of search results found on the search results template, you can find the value in the variable $wp_query->found_posts. Just add

<?php echo '<p>Found ' . $wp_query->found_posts . ' hits.</p>'; ?>

in your search results template. In case that produces no results, try introducing the global $wp_query variable like this:

<?php global $wp_query; echo '<p>Found ' . $wp_query->found_posts . ' hits.</p>'; ?>

The usual method which involves a new WP_Query object does not work with Relevanssi.

4 comments Showing the number of search results

  1. This doesn’t seem to be the # of results, but the number of times that search term was found. Therefore if you search for a term and one page has the term 34 times, the output would be 34 even though there is one result in the list.

  2. $query->found_posts is the number of documents with hits. I tried this on my blog, and the number shows the amount of results and didn’t change, even if I added the search term in the documents.

  3. Hello Mikko,
    if Relevanssi is active, the search result yields less posts than the normal WP search. The difference is high sometimes, eg. without Relevanssi “59 posts found”, with Relevanssi “14 posts found”.

    Posts to be indexed are “all public post types”. There are no other exclusions and restrictions (if I see it correctly).

    Are there any other magic filters? What is the reason for such a difference?

Leave a Reply to Mikko Saari Cancel reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *