I would like to get posts by their deepest categories.
$posts = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'custom_post_type',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'custom_category',
'field' => 'term_id',
'terms' => (Main term id)
),
array(
'taxonomy' => 'custom_category',
'field' => 'term_id',
'terms' => (Child term id)
),
array(
'taxonomy' => 'custom_category',
'field' => 'term_id',
'terms' => (Grandchild term id)
)
)
)
);
For example:
(Main term id) = 1
(Child term id) = 2
(Grandchild term id) = 3
I would like to get only the following posts:
- Post has only the Main category (1) and doesn't have any Child or Grandchild category.
- Post has the Main category (1) and has the Child category (2) and doesn't have any Grandchild category .
- Post has the Main category (1), has the Child category (2) and has the Grandchild category (3) too.
Is it possible? Thank you for your help!