How to limit page pagination... again

I'm digging out on old question as no solution came out. How to limit page pagination

I'm looking for a way to limit pages on homepage and categories (archive.php). I have around 200k posts spread into about 50 categories. If I leave the default pagination, homepage has 20k pages. Then most categories have over 5k pages. This will send google in some long long loops... and each time a post is added (200/day) google will start again etc... when all I want is google to look into recent posts (this is a news website)

All I need is to let the visitors navigate threw the 10 first pages of each categories.

Is there a nice way for doing this?

I've tried different things described here without success. Limit number of pages in pagination

// this didn't work (beyond page 10 "next page" take us back to page 1)
function wpcodex_filter_main_search_post_limits( $limit, $query ) {     
  return 'LIMIT 0, 100'; 
} 
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );

Removing the page links on page 10 would work for me even if you can manually enter page 11 and see page 11 posts... all I want is google not to find a link.

Deleting old posts is not an option, nor running a double query.

Thanks for your help

Topic limit pagination Wordpress

Category Web


You can use the following function into your archive template

paginate_links

And set "total" to your desired number of pages.

Ref: https://developer.wordpress.org/reference/functions/paginate_links/


I was lucky, the theme used it's own function, so it was easy to override. Inspired by worpdress native get_the_posts_navigation (wp-includes/link-template.php) here is what I end up using:

function my_get_the_posts_navigation( $args = array() ) {
    $limit = 5;
    $navigation = '';

    // Don't print empty markup if there's only one page.
    if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
        $args = wp_parse_args( $args, array(
            'prev_text'          => __( 'Older posts' ),
            'next_text'          => __( 'Newer posts' ),
            'screen_reader_text' => __( 'Posts navigation' ),
        ) );


        $next_link = get_previous_posts_link( $args['next_text'] );

        $p = (get_query_var('paged')) ? get_query_var('paged') : 1;
        if ($p < $limit) {
          $prev_link = get_next_posts_link( $args['prev_text'] );
        } else {
          $prev_link = false;
        }

        if ( $prev_link ) {
            $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
        }

        if ( $next_link ) {
            $navigation .= '<div class="nav-next">' . $next_link . '</div>';
        }

        $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
    }

    return $navigation;
}

function my_the_posts_navigation( $args = array() ) {
    echo my_get_the_posts_navigation( $args );
}

// now override the theme pagination function
if ( ! function_exists( 'cactus_paging_nav' ) ) :
function cactus_paging_nav() {

    my_the_posts_navigation();
}
endif;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.