How to display the searched term in WordPress search.php template

When users search for something on a WordPress site the system sends them to a search results page that is rendered using the Search Results (search.php) page. Depending on which theme you are using for your WordPress site the text on this search.php file may vary. One thing I always like to do on my sites is show users what they searched for at the top of the search results.

Many basic WordPress themes will simply display “Search Results” at the top of the page, but if you want to make it fancier just follow these steps:

  1. In WordPress go to Appearance > Editor > Search Results (search.php)
  2. Make sure your templates are editable, if not, check out this tutorial then come back here
  3. Find the area in the template that says “Search Results” or some other message, this is usually contained in <h2> tags
  4. Add the following line of code to have WordPress automatically display the term or phrase the user searched for <?php the_search_query(); ?>
  5. You can also add this code to the “Nothing Found” search results, I like to change mine to look like this: <h2>No posts found containing “<?php the_search_query(); ?>“. Please try a different search.</h2>

That’s it! Now you can click Update File at the bottom of the page and visit your site to see how it works. Now when you search for something the results page has more useful information about what you were looking for.