Displaying search results in a custom WordPress theme

I’m stuck trying to get search results to show up in my custom WordPress theme. I’ve added the searchform.php to my header and created an empty search.php file. When I search, it takes me to a blank page.

I think I need to add something to search.php, but I’m not sure what. I’ve looked through the WordPress Codex but couldn’t find a clear answer.

Does anyone know what I need to do to make search results appear? Is there a specific function or query I should use?

Here’s my searchform.php code:

<form action="<?php echo home_url(); ?>" id="search-form" method="get">
    <input type="text" name="s" id="search-input" placeholder="Enter search terms">
    <button type="submit">Search</button>
</form>

Any help would be great. I’m really stuck on this!

hey grace, i had the same issue. you need to add the loop to search.php. try this:

<?php
if (have_posts()) :
    while (have_posts()) : the_post();
        // display post content
    endwhile;
else :
    echo 'No results found';
endif;
?>

this should display your search results. good luck!

Hey Grace, I’ve been through this exact headache before. The key is definitely in your search.php file. Here’s what worked for me:

First, make sure you’re calling get_header() at the top of search.php. Then, you’ll want to set up your main content area and include the WordPress loop. Something like this:

<?php get_header(); ?>

<main id="main-content">
    <h1>Search Results for: <?php echo esc_html(get_search_query()); ?></h1>

    <?php
    if (have_posts()) :
        while (have_posts()) :
            the_post();
            get_template_part('template-parts/content', 'search');
        endwhile;

        the_posts_pagination();
    else :
        echo '<p>No results found. Try a different search?</p>';
    endif;
    ?>
</main>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Don’t forget to create a content-search.php file in your template-parts folder to control how each search result is displayed. This approach gives you more flexibility in styling your results. Hope this helps!

Grace, I’ve encountered this issue before. Your search.php file needs to include the WordPress loop to display results. Here’s a basic structure to get you started:

<?php get_header(); ?>

<h1>Search Results for: <?php echo get_search_query(); ?></h1>

<?php
if (have_posts()) :
    while (have_posts()) :
        the_post();
        // Your theme's post display code here
        // For example: get_template_part('content', 'search');
    endwhile;
    the_posts_navigation();
else :
    echo '<p>Sorry, no posts matched your search.</p>';
endif;
?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Customize this to match your theme’s structure and styling. Remember to create a content-search.php file if you use get_template_part().