Filter CPT posts by one or more categories
Question
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(
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?
0
categories, custom-post-types, custom-taxonomy
3 years
2019-12-07T17:45:14-05:00
2019-12-07T17:45:14-05:00 0 Answers
71 views
0
Leave an answer