1

I am trying to display search results in search.php , but no results showing. it just show this search results for: then the keyword I put in the search box only nothing more. so the actual results for the keyword not showing from the posts.

I have two files. here are

search.php

<?php get_header();  ?>

     <h5><b>SEARCH RESULTS</b></h5>
     <h3 class="mt-30 mb-15">SEARCH RESULTS FOR YOUR KEYWORD</h3>



<h1><?php printf( __( 'Search Results for: %s'), '<span>' . get_search_query() . '</span>' ); ?></h1>


<?php get_footer();  ?>

and searchform.php

<form action="/" method="get">
    <label for="search">Search in <?php echo home_url( '/' ); ?></label>
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
</form>


so what should I do?

-1

get_search_query() returns only the string that the user searched for. To display the search results, you'll need to do some more work.

The search results will be in $wp_query, and you can manipulate it using The Loop (like any other page).

The following code is adapted from WordPress's own Twenty Twenty theme's index.php file, and can be adapted for your theme's search.php file.

if ( is_search() ) {
        global $wp_query;

        $archive_title = sprintf(
                '%1$s %2$s',
                '<span class="color-accent">' . __( 'Search:', 'twentytwenty' ) . '</span>',
                '&ldquo;' . get_search_query() . '&rdquo;'
        );

        if ( $wp_query->found_posts ) {
                $archive_subtitle = sprintf(
                        /* translators: %s: Number of search results. */
                        _n(
                                'We found %s result for your search.',
                                'We found %s results for your search.',
                                $wp_query->found_posts,
                                'twentytwenty'
                        ),
                        number_format_i18n( $wp_query->found_posts )
                );
        } else {
                $archive_subtitle = __( 'We could not find any results for your search. You can give it another try through the search form below.', 'twentytwenty' );
        }
}

if ( have_posts() ) {

        $i = 0;

        while ( have_posts() ) {
                $i++;
                if ( $i > 1 ) {
                        echo '<hr class="post-separator styled-separator is-style-wide section-inner" aria-hidden="true" />';
                }
                the_post();

                get_template_part( 'template-parts/content', get_post_type() );

        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.