How to display Wordpress search results?

How to display Wordpress search results?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

How to display Wordpress search results

Creating a custom theme often involves overriding default template files to match your design system. When you replace search.php with an empty file or rely solely on including searchform.php, the most common issue isn't the form itself, but rather how WordPress handles the query execution after the form submission.

In your specific setup, the searchform.php file looks standard enough for a basic input field. However, simply creating an empty search.php template results in a blank page because WordPress expects that template to contain logic for querying the database and rendering the output. The Codex suggests creating a search page, but it doesn't explicitly detail the PHP logic required inside the custom template file to actually fetch and display posts.

To resolve this, you need to understand how WordPress processes the WP_Query. When a user submits your form, the term is passed via the s query variable. Your search.php template must initialize a new WP_Query object using this term and then loop through the results to output them.

Here is the fundamental structure required inside your search.php file:

<?php get_header(); ?>

<div id="main">
    <div id="content">
        <?php if ( is_search() ) : ?>
            <!-- Your search form should go here, or include searchform.php -->
            <?php get_template_part( 'searchform' ); ?>

            <h1 class="page-title">Search Results</h1>

            <?php if ( have_posts() ) : ?>
                <?php while ( have_posts() ) : the_post(); ?>
                    
                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <div class="entry-summary">
                            <?php the_excerpt(); ?>
                        </div>
                    </div>

                <?php endwhile; ?>

            <?php else : ?>
                <p>No results were found matching your search criteria.</p>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>

<?php get_footer(); ?>

The critical missing piece in your original attempt was the if ( is_search() ) block and the subsequent WP_Query. If you want to customize the search results further, such as changing the number of posts per page or excluding certain post types, you would modify the query arguments passed to the new WP_Query() constructor.

One common pitfall when developing custom themes is forgetting to ensure the search form actually submits correctly. In your provided code snippet, the form action points to home_url(). While this works for most standard setups, WordPress often prefers the search term to be handled via a global POST or GET request that triggers the specific template hierarchy. If your theme relies on redirects or AJAX calls, ensure the form method aligns with how your server handles requests.

Another consideration is pagination. By default, WordPress displays 10 results per page for search queries. If you are displaying more or fewer items, you must adjust the posts_per_page argument in your custom query. Failure to do so can lead to broken pagination links if you decide to implement them later using wp_pagenavi() or a similar plugin.

If your current empty search.php continues to show nothing, double-check that the file is actually being loaded. You can verify this by placing a temporary header comment at the top of the file: <?php // Custom Search Template Loaded ?>. If this doesn't appear, your theme might not be recognizing the template hierarchy correctly, or you may need to clear any caching layers.

For developers managing complex enterprise sites where maintaining multiple WordPress instances alongside other frameworks like Laravel is necessary, understanding these core templating mechanics remains essential. Resources like LaravelCompany often discuss the intersection of PHP development ecosystems, but for standard WordPress theming, sticking to the built-in WP_Query and template hierarchy is the most robust approach.

Our network