On my site I've created a CPT with a hierarchical custom taxonomy:
// **CPT Args** $args = [ 'labels' => $labels, 'public' => true, 'publicly_queryable' => false, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => ['slug' => 'documento'], 'taxonomies' => ['tipologia'], 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => ['title'] ]; //**Taxonomy Args** $args = [ 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'rewrite' => ['slug' => 'documenti' ], ];
Here the code for pagination:
echo paginate_links( [ 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var( 'paged' ) ), 'total' => $wp_query->max_num_pages ] );
The strange issue is that the pagination works well with some terms:
Example:
documenti/altri-documenti/comunicati/cs-2015/page/2/ <- it works (it doesn't have child terms)
documenti/altri-documenti/comunicati/page/2/ <- not works (this term have children) return 404
documenti/altri-documenti/memore/page/2/ <- it works (this term have children)
documenti/altri-documenti/memore/page/3/ <- not works (this term have children) return 404
documenti/formulari-moduli/page/2/ <- it works (it doesn't have child terms)
It seems like the issue is only with parent terms and not with the last child term.
EDIT
The issue is also with permalinks disabled:
/?tipologia=altri-documenti&paged=2
<- this does not work
query_posts
, it breaks the main query, it is slow and totally stuffs up pagination. Usepre_get_posts
to alter the main query to your needs. There are so many info aboutpre_get_posts
on site, just use the site search