Skip to main content
5 of 7
added 153 characters in body
DevelJoe
  • 497
  • 6
  • 21

Define your terms as hierarchical custom taxonomies, as child taxonomies from the corresponding parent taxonomy you want them to inherit from...? Like this you would only need to query for your parent tax, and simply echo out the respective child taxonomies, if required.

** UPDATE **

What you do is query all the terms of a given taxonomy, and use them to query posts having them. What you should do instead is simply query posts which have terms of the brand taxonomy associated to it (and whichever additional feature you may want to use for the query), and while retrieving these posts (inside your wp_query loop), access the id of the currently queried post in the loop, and retrieve the brand terms associated to it. Sth like this:

$product_query_args = array(
  'post_type' => 'your_post_type',
  'tax_query' => array(
    array(
      'taxonomy' => 'brand'
    )
  )
);

$product_request = new WP_Query( $product_query_args );

if ( $products_request->have_posts() ) {
  
  while ( $products_request->have_posts() ) {

    $products_request->the_post();

    // Get ID of currently iterated post
    $id_of_iterated_product = get_the_ID();

    // Retrieve associated brand terms
    $brands = get_the_terms( $id_of_iterated_product, 'brand' );

    // Check if there are brand terms associated to iterated post
    if ( ! empty( $brands ) ) {

      // If so, iterate through them and get the name of each
      foreach( $brands as $brand_key => $brand_object ) {

        $brand_name = $brand_object->name;

        // Do whatever you want with it

      }

    }

  }

}

Like this, you actually only make one full query instead of two, so performance should be better, hopefully. Let me know how this solution suits you.

DevelJoe
  • 497
  • 6
  • 21