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 |2 Answers
Reset to default 1Try 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.
__not_in
style queries – Tom J Nowell ♦ Commented Feb 24, 2019 at 2:37