$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - Change the number of excerpts displayed in search results layout|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - Change the number of excerpts displayed in search results layout

matteradmin10PV0评论

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?

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?

Share Improve this question edited Mar 2, 2019 at 21:40 dstonek asked Mar 2, 2019 at 15:32 dstonekdstonek 1035 bronze badges 2
  • 2 You are missing the s parameter in the WP_Query call. See codex.wordpress/Class_Reference/WP_Query#Search_Parameter – czerspalace Commented Mar 2, 2019 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 Commented Mar 2, 2019 at 22:19
Add a comment  | 

1 Answer 1

Reset to default 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');
Post a comment

comment list (0)

  1. No comments so far