Mar
20
2009

Show Your Search Query in WordPress


Wouldn’t you like to be able to display the actual search term entered by your user on the search results’ page? Good thing there is a very simple way in WordPress to do that with the_search_query function. Just open up your search.php file and enter:

<?php the_search_query(); ?>

to display the search term that was entered. So something like this:

<?php echo 'Search Results for "'; the_search_query(); echo '"'; ?>

would display something that looked like this:

Search Results for “the search term”

Another cool little trick it to add the number of search results. Use this code:

<?php
$mySearch =& new WP_Query("s=$s & showposts=-1");
$num = $mySearch->post_count;
echo $num.' search results for "'; the_search_query();
?>

to display something like:

18 search results for “the search term”

If you liked this, please share it.

Tags: , , , , , , , , , , , , , , , , ,

Short URL: http://bit.ly/aX2Owp

Discussion 3 Comments

  1. Promosi Indonesia on April 4, 2009 at 8:47 pm

    Thanks for the tips !

    Best Regards

  2. paul on February 11, 2010 at 1:56 pm

    Awesome! This is exactly what I needed. Thanks!

  3. CHris on April 23, 2010 at 11:28 pm

    Thanks

Leave a Reply

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

*


To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.