Count your wordpress search results

The default WordPress search results page is noting but to show you the total results as per your search keywords. You can spice this page up a bit by showing the total search count like
4 Results found or something like this….

Just you need to add some codes and it will look like the image below.

The main code which do this action is:

<?php $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; ?>

Now what you will have to do is as follows.

Step 1: Open search.php page inside your theme folder. Remove the existing search query and put the following code.

Step 2: Save the file and check the updates doing some search.

If you are new to WordPress theme modification then make sure you will not basic WordPress template function like header, sidebar and footer as get_header(); get_sidebar(); and get_footer();

You can modify the code for style purpose or adding more ingredients on search.


Posted

in

by

Tags:

Comments

4 responses to “Count your wordpress search results”

  1. Jasper avatar
    Jasper

    An easier and better way to do this is …

    found_posts; ?>

    That saves an extra database call.

    1. Jasper avatar
      Jasper

      Hmm, not all code is shown. Again, without code tag:

      found_posts; ?>

      1. Jasper avatar
        Jasper

        global $wp_query;
        echo $wp_query->found_posts;

        1. Sanjay Bhowmick avatar
          Sanjay Bhowmick

          Thank you Jasper, you are right.

Leave a Reply

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