1

we have a custom WP_QUERY with these args: https://pastebin.com/drbw0NAG

$args = [
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => $post_number + 1,
    'paged'          => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
    'tax_query'      => [
        [
            'taxonomy'         => $taxonomy,
            'field'            => 'term_id',
            'terms'            => $category_id,
            'include_children' => false
        ],
    ],
];

We'd like to exclude those post which are also assigned to a child category.

I mean, we have some terms:

-- Parent 1

-- Parent 2

------ Child 1

-- Parent 3

We'd like to get posts in Parent 2 but not those are also assigned to Child 1.

Is it possible? Have you got any suggests?

Thanks

0

You'll need a second tax query for posts not in the child categories of the given category. To do that you'll need to get the IDs of the children. This is possible with get_term_children():

$args = [
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => $post_number + 1,
    'paged'          => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
    'tax_query'      => [
        [
            'taxonomy'         => $taxonomy,
            'field'            => 'term_id',
            'terms'            => $category_id,
            'include_children' => false
        ],
        [
            'taxonomy' => $taxonomy,
            'field'    => 'term_id',
            'terms'    => get_term_children( $category_id, $taxonomy ),
            'operator' => 'NOT IN'
        ],
    ],
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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