最新消息: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)

How to modify query so it grabs only 90 posts in total?

matteradmin5PV0评论

I want to have 3 Pages with 30 posts each: Total 90 posts. /page/4/ shouldn't exist. Should either 404 or redirect to home.

Only /, /page/2/ and /page/3/ should exist. With 30 posts each, like so:

  • / posts -> 01-30
  • /page/2/ posts -> 31-60
  • /page/3/ posts -> 61-90

I've tried numerous suggestions, none limit the query for me. Just the number of posts per page. This looked promising but has no effect (/page/999/ works)

function wpcodex_filter_main_search_post_limits( $limit, $query ) {

    if ($query->is_front_page()) {
//this:
return '90';
//or even this: 
return 'LIMIT 90';
    }

    return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );

Some people as a work around suggest counting with PHP and using an if to stop showing the posts. That's a work around i want to limit the SQL query.

I want to have 3 Pages with 30 posts each: Total 90 posts. /page/4/ shouldn't exist. Should either 404 or redirect to home.

Only /, /page/2/ and /page/3/ should exist. With 30 posts each, like so:

  • / posts -> 01-30
  • /page/2/ posts -> 31-60
  • /page/3/ posts -> 61-90

I've tried numerous suggestions, none limit the query for me. Just the number of posts per page. This looked promising but has no effect (/page/999/ works)

function wpcodex_filter_main_search_post_limits( $limit, $query ) {

    if ($query->is_front_page()) {
//this:
return '90';
//or even this: 
return 'LIMIT 90';
    }

    return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );

Some people as a work around suggest counting with PHP and using an if to stop showing the posts. That's a work around i want to limit the SQL query.

Share Improve this question asked Feb 22, 2019 at 22:53 Michael RogersMichael Rogers 5498 silver badges37 bronze badges 4
  • 1 What problem does this solve for you? Can you provide some context? – Tom J Nowell Commented Feb 23, 2019 at 0:26
  • @TomJNowell with 17,000 posts the home page main query is a slow query. – Michael Rogers Commented Feb 23, 2019 at 9:43
  • hmmm I don't think this will do much for your performance if you have a paginated homepage, what you're asking for is equivalent to just pretending there are only 3 pages. Keep in mind that some queries scale very well with post counts. Others scale catastrophically ( e.g. anything involving post meta, or __not_in style queries – Tom J Nowell Commented Feb 24, 2019 at 2:37
  • I ended up leaving default behavior (all posts, 20 per page). But installed elasticsearch server with elasticpress plugin i can do " 'ep_integrate' => true," and bypass mysql altogether. It's way faster. – Michael Rogers Commented Feb 25, 2019 at 11:17
Add a comment  | 

2 Answers 2

Reset to default 1

Try using paginate_links( $args ). Here is code adopted from codex.

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
    'posts_per_page' => 30,
    'paged' => $paged,
);

$the_query = new WP_Query( $args );

// the loop etc goes here.. 

$big = 999999999; 

echo paginate_links( array(
    'base'       => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format'     => '?paged=%#%',
    'current'    => max( 1, get_query_var('paged') ),
    'prev_next'  => false,
    'total'      => 3
) );

See detail here on codex.

Your solution doesn't work because it is limiting the current loop to 90 records. It doesn't limit the total records.

For your problem, you can just cap the paged value before WordPress does its main loop like this.

function override_paged(){
    if(get_query_var('paged')>3){

        //Cap the value
        set_query_var( 'paged', 3 ); 

        //Or redirect
        //wp_safe_redirect('HOME_PAGE_URL');die();

    }
}
add_action('pre_get_posts','override_paged');

This will affect all WordPress query, so please adjust to your requirements.

Post a comment

comment list (0)

  1. No comments so far