Let's say I have custom post type article.

I have a custom taxonomy fruit, with terms like apples.

And another custom taxonomy company, with terms like juicy-inc and crunchy-co.

Both are associated with article. So articles can be tagged with fruit and company.

I also have a theme file for fruit, taxonomy-fruit.php, which shows the page for, say, Apples (http://www.domain.com/fruit/apples).

On this page, what I want to do is show lists of articles, but grouped by company.

ie. For each company term, actually only those which have any articles with a taxonomy fruit term corresponding to the current term, show a post list.

So, when the page is Apples, I would like to see...

Term page title: Apples

Term description would go here. Term description would go here. Term description would go here. Term description would go here. Term description would go here.

Juicy Inc

  • Article about apple picking season.
  • Article about 5 best apples.
  • Article about Juicy Inc's secret orchard.

Crunchy Co

  • Article about Crunch Co's apple history.
  • Article about making cider.
  • Article about Crunchy Co's founder.

... and so on, for as many company terms as have an article tagged with the fruit term of the current taxonomy-fruit.php

I'm unsure whether this is about starting with a query for the current term (since the page is already there) and somehow interrogating it to split it up, or alternatively cycling through a query that starts with company.

And I will be interested that a) the output order of those company terms can be influenced in some way, ie. by descending volume of articles tagged with the current fruit, and b) a limit/pagination might be placed on the company segments, since there is likely to be a large number of company terms.

I'm a bit lost, really.


This seems really close...

... as I speculated, start by getting company terms and, for each one, do a query and loop through to output.

The output is slow - I don't know whether that's because this is an inefficient method or because I have hundreds of company terms.

Any comments about this method vs another?

Also, it lacks output ordering on company.

    $company_terms = get_terms( 'fruit' );

    foreach ( $company_terms as $company_term ) {
        $company_query = new WP_Query( array(
            'post_type' => 'article',
            'tax_query' => array(
                    'taxonomy' => 'company',
                    'field' => 'slug',
                    'terms' => array( $company_term->slug ),
                    'operator' => 'IN'
                    'taxonomy' => 'fruit',
                    'field' => 'slug',
                    'terms' => 'apple',
                    'operator' => 'IN'
        ) );

        if ( $company_query->have_posts() ) { ?>
          <h2><?php echo $company_term->name; ?></h2>
            <?php while ( $company_query->have_posts() ) {
              $company_query->the_post(); ?>
              <li><?php echo the_title(); ?></li>
            <?php } ?>
        <?php }

        // Reset things, for good measure
        $company_query = null;

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.