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()) {
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.

  • 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. 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. Commented Feb 25, 2019 at 11:17

2 Answers 2


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(){

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

        //Or redirect


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

  • Hi thanks. It does it but the pagination links underneath still show all the pages instead of just the 3, What can i do? Commented Feb 23, 2019 at 13:27
  • 1
    Solved with @Qaisar Feroz idea of using 'total' => 3 on the pagination links. Else it would offer all the pages as options. Commented Feb 23, 2019 at 13:36
  • pre_get_posts takes a query object in as a parameter, it should be using that object to set query variables. That way it can also check if it's the homepage, and if it's the main query
    – Tom J Nowell
    Commented Feb 24, 2019 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.