Mar
20
2009

Show Your Search Query in WordPress

by   |  Posted in Tutorials  |  5 comments

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”

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

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

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

Discussion 5 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

  4. wp magz on September 13, 2010 at 8:12 am

    cool very helpful :)

  5. tech on April 9, 2011 at 10:45 pm

    nice code, very useful thank a lot for share :)