0

WP 5.1 TwentySeventeen (child) theme.

_posts_per_page = 1

For search results I want to display 5 excerpts.
Using this answer and this page I was able to do that but displayed excerpts are not from search results, they are the last 5 published posts.

This is how the loop looks in modified search.php:

    <?php 
    $blogpost = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 5));
    if ( $blogpost->have_posts() ) :
    //if ( have_posts() ) :
        /* Start the Loop */
        while ( $blogpost->have_posts() ) :
        //while ( have_posts() ) :
            $blogpost->the_post();
            //the_post();

            /**
             * Run the loop for the search to output the results.
             * If you want to overload this in a child theme then include a file
             * called content-search.php and that will be used instead.
             */
            get_template_part( 'template-parts/post/content', 'excerpt' );

        endwhile; // End of the loop.    
        wp_reset_postdata();

And the pagination code:

        the_posts_pagination(
            array(
                'prev_text'          => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
                'next_text'          => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
                'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
            )
        );

Number of excerpts displayed are okay but they are not taken from Search Results (despite the keyword entered, the output is always the same, the last 5 published posts).

Note: With modified search.php pagination corresponds to the real search results. Because default posts per page = 1 if real search result finds 3 posts so 1·2·3 is displayed at bottom.

How can I output a desired number of excerpts from search results and get pagination according to that?

  • 2
    You are missing the s parameter in the WP_Query call. See codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter – czerspalace Mar 2 at 21:49
  • I see, thanks. So $blogpost = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 5, 's' => get_search_query())); How to fix pagination? I still see 1·2·3 despite the 3 results are in the first page. If I have more than 5 results second page output is the same as the first. – dstonek Mar 2 at 22:19
1

When you search on wp it automatically does a search query. In your code you make a new WP_Query to show 5 posts (which sorts by data by default) and there is no need to do that.

The best thing to do here is to leave the search.php file as it is and use an action hook. You can add this to your child theme functions.php file.

function my_search_filter( $query ) {
  if ( $query->is_main_query() ) {
    if ( $query->is_search ) {
      $query->set('posts_per_page', '5');
    }
  }
}

add_action('pre_get_posts','my_search_filter');

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.