I'm trying to filter the posts on a CPT by one or more categories.

Here's what I have so far:

$args = array(
    'post_type'         => 'cpt-name',
    'posts_per_page'    => -1,

// cat_term_id have been registered as a custom query var
$categories = get_query_var( "cat_term_id" );

if ( !empty( $categories ) ) {    
    $args['tax_query'] = array(
            'taxonomy'          => 'category',
            'field'             => 'term_id',
            'terms'             => $categories,
            'include_children'  => false,

$filtered_posts = get_posts( args );

The query works, the problem is that is bringing all the posts on the CPT, and what I want is to only get the posts using the categories passed by cat_term_id.

Am I missing something?

1 Answer 1


Actually, the query was working as expected.

The problem was that with the pagination call:

    'screen_reader_text' => 'Navigation',
    'prev_text'          => '<i class="fa fa-angle-left" ></i>',
    'next_text'          => '<i class="fa fa-angle-right" ></i>',

From reading the code and finding this question on StackOverflow, I discovered that the function the_posts_pagination is a wrapper for the function get_the_posts_navigation which uses $GLOBALS['wp_query']->max_num_pages to know the number of pages to paginate. And because I'm using a custom query, my pagination was out of context.

I hope this helps someone.

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.