0

I want to query a custom taxonomy by the title of articleTopics. This taxonomy has two parent topics, and each parent topic has the same subset of child-topics. The user will select a sub-topic for each post. Imagine it like this:

Topic1:
  Sub-topic1
  Sub-topic2
  Sub-topic3
Topic2:
  Sub-topic1
  Sub-topic2
  Sub-topic3

My tax query looks as follows:

$topic_articles = array(
    'post_type' => 'vet',
    'tax_query' =>  array(
        'relation' => 'OR',
        array(
            'taxonomy'  => 'articleTopics',
            'field'     => 'term_id',
            'terms'     => '34'
        ),
        array(
            'taxonomy'  => 'articleTopics',
            'field'     => 'term_id',
            'terms'     => '35
        )
    ),
);
$articles = new WP_Query($topic_articles);

Now, obviously this returns all articles which match either parent, and there is no way to know which post belongs to which parent.

I need to find a way to query all posts which match any of the sub-categories of Topic1, and Topic2, ideally within one Query. I need to be able to separate the results because I want to display them separated by parents on the front-page:

Topic1:
  Post1
  Post2

Topic2:
  Post1
  Post2

I am using Query Monitor and am already up to 33 queries, so ideally i'd like to keep all logic in the same query. Is this possible?

Your Answer

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

Browse other questions tagged or ask your own question.