最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论