1

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

5
  • You are in all propability running a custom query in place of the main query Oct 21, 2015 at 8:24
  • In the taxonomy.php page I needed to add this: query_posts( array_merge( $args, $wp_query->query ) ); and adding some extra query args to the main query, but why for some terms works?What is your suggestion?removing the main query and add only a custom query? Oct 21, 2015 at 8:37
  • 1
    NEVER EVER use query_posts, it breaks the main query, it is slow and totally stuffs up pagination. Use pre_get_posts to alter the main query to your needs. There are so many info about pre_get_posts on site, just use the site search Oct 21, 2015 at 8:39
  • 1
    Thank you, using pre_get_posts instead of query_post fixed the issue... Oct 21, 2015 at 10:00
  • My pleasure, glad you solved it. ;-). Enjoy Oct 21, 2015 at 10:26

1 Answer 1

1

user2660802 reports that the problem has been resolved thanks to using Pieter Goosen's suggestion of using pre_get_posts instead of query_posts().

query_posts() breaks the main query, it is slow, and totally stuffs up pagination.

Your Answer

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

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