How to show search results in custom WordPress theme?

I’m working on a custom WordPress theme and having trouble getting search functionality to work properly. I managed to add the search form to my header and created a search.php template file, but it’s currently empty. When someone searches, they just see a blank page.

I know I need to add something to my search.php file to actually display the search results, but I’m not sure what code to use. What functions or queries do I need to show the search results?

Here’s my current search form code:

<form method="get" action="<?php echo esc_url(home_url('/')); ?>" id="searchform">
    <input type="text" name="s" id="search-input" placeholder="Enter search terms" 
           onblur="if(this.value=='')this.value='Enter search terms'"
           onfocus="if(this.value=='Enter search terms')this.value=''" />
    <input type="submit" value="Search" />
</form>

What should I put in my search.php template to make the results show up?

check that search.php is in your theme’s root folder, not buried in templates/ or another subfolder. i’ve seen people mess this up constantly. start simple - just throw in echo “search works” to make sure the file’s even loading before you add all the loop code.

Check that you’re calling the right template hierarchy in WordPress. If search.php isn’t loading, there’s probably something wrong with your theme structure. Your code looks good, but add pagination since search results can get long:

the_posts_pagination(array(
    'mid_size' => 2,
    'prev_text' => 'Previous',
    'next_text' => 'Next',
));

Watch out for styling issues too. Search results often look broken because they’re inheriting styles from regular posts. I learned this the hard way when my search page looked completely different from everything else.

One gotcha: WordPress search doesn’t include custom fields by default. If you need to search custom meta data, you’ll need the pre_get_posts hook to modify the search query. This matters if you’re building a product catalog or directory where users expect to find stuff based on custom field values.

Had this exact same problem switching from a pre-built theme to custom dev. Blank page usually means your search.php exists but WordPress can’t find it properly. Check your file encoding first - save as UTF-8 without BOM if you’re on Windows. Make sure there’s no whitespace before your opening PHP tag too, that’ll break template loading.

Besides the basic loop structure others mentioned, wrap your search results in a container div that matches your main content styling. Your search form has inline JavaScript for placeholder text, but modern browsers handle the placeholder attribute on their own now - you can ditch that code.

One thing that bit me: search excerpts looked terrible when posts had shortcodes or special characters. Use wp_trim_excerpt() instead of get_the_excerpt() for cleaner output. Test with different search terms including single characters and common words to make sure it all displays right.

The loop structure works, but WordPress search becomes a nightmare with real traffic and content.

Hit this exact problem last year when our site started timing out on searches. Built-in search hammered the database too hard and users got garbage results.

Skipped trying to fix WordPress search - built a workflow that syncs content to a proper search service when posts get published or updated. No more slow database queries.

Workflow triggers on post saves, grabs content and metadata, sends it to the search service. Users get instant results with actual ranking and filtering.

Works with your existing search.php template. Just change the form action to hit your endpoint instead of WordPress default.

Scales way better than optimizing WordPress queries, plus you get typo tolerance and advanced filtering built-in.

The previous responses nailed the basic loop structure, but here’s what trips up tons of developers - search result formatting consistency. When I built my first custom theme, my search results looked nothing like my regular post listings because I forgot to reuse existing template parts. Create a separate content-search.php template part and call it with get_template_part('content', 'search') inside your search loop. This keeps your search results styled consistently with the rest of your site. WordPress search is case-sensitive by default and sucks at partial matches. Users constantly complain when they can’t find content they know exists. If you’re launching this theme publicly or for clients, modify the search query with posts_search filter to make it more user-friendly from the start.

Your search form looks fine, but that blank search.php is your problem. You need a basic WordPress loop to show results.

Here’s what goes in your search.php:

get_header();
if (have_posts()) {
    echo '<h1>Search Results for: ' . get_search_query() . '</h1>';
    while (have_posts()) {
        the_post();
        echo '<div class="search-result">';
        echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
        echo '<p>' . get_the_excerpt() . '</p>';
        echo '</div>';
    }
} else {
    echo '<h1>No results found</h1>';
    echo '<p>Try different keywords.</p>';
}
get_footer();

This covers the basics, but WordPress search is pretty limited and gets slow on bigger sites.

I ditched native WordPress search entirely on my last project. Used Latenode instead to handle search requests and connect to external APIs like Algolia or Google Custom Search. Much more powerful with better relevance scoring.

The automation indexes content when posts get published or updated. Users get lightning fast results and advanced filtering that WordPress can’t match.