Show Your Search Query in WordPress

Posted on March 20, 2009  |  Category: Tutorials

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”


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

Short URL: http://bavotasan.com/?p=396

2 Responses to “Show Your Search Query in WordPress”

  1. Thanks for the tips !

    Best Regards

    #1509
  2. Awesome! This is exactly what I needed. Thanks!

    #7645

Leave a Reply

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.