Separate results from taxonomy query

by Hewe   Last Updated August 14, 2019 15:08 PM

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?



Related Questions


CPT archive admin menu label

Updated April 12, 2018 10:08 AM

How to use a custom post type archive as front page?

Updated November 26, 2017 10:08 AM

wp_insert_post not working with custom_post_type

Updated April 23, 2015 00:03 AM


Getting multiple custom post_types in one url

Updated May 21, 2017 11:08 AM