Group posts that matches a term in a loop

Question

I’m working on a website that shows a lot of products, and this is my structure:

  • Product (custom post type)
  • –>Brand (custom taxonomy)
  • ——>Brand A (custom term)
  • ——>Brand B (custom term)
  • –>Department (custom taxonomy).
  • ——>Department A (custom term)
  • ——>Department B (custom term)

I need to show which brands are in each department, and the only way to link them is using my products (because each custom taxonomy is independent from each other). I’m able to do it by quering all the products of a specific department, and retriving the brand of each product (instead of its name). This is the code I use to perfom this task:

<?php
    $args = array(
     'tax_query' => array(
         array(
             'taxonomy' => 'department',
             'field' => 'slug',
             'terms' => array( 'department-a' )
         ),
     ),
     'orderby' => 'title',
     'order' => 'ASC',
     'post_type' => 'prod'
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {

     $term = $query->queried_object;

     while ( $query->have_posts() ) : $query->the_post(); ?>

         <li>
                  <?php
         // Get brand for post
         $terms = get_the_terms( $post->ID , 'brand' );    

         foreach( $terms as $term ) {
            print $term->name . ' <br />';
            unset($term);
         }
         ?>

       </li>
    <?php
    endwhile;

    wp_reset_query();
    } ?>

However, as some of my products has the same Brand, I need to group them if this happen. So my output sould be something like: DEPARTMENT A: Brand A has 3 products, Brand B has 1 product, Brand C has 2 products, etc.

Do you have any idea of how can I acheive this? Thanks.

EDIT

Capiadge answer let me know how many products are in a brand, but I can’t access to those products. I’ve made a dropdown that matches all my needs with Brands (it checks if my brand has one or more product, if it’s one, it sets a href attribute that I get via ajax and jquery so the user can go the the product page; if it’s more, it sets a value that I get so if the user clics it may see which products are in that brand). The only thing that I need and I can’t achive is to do the same thing, but not get all the Brands, but the Brands that have products in only one specific department. (i.e: a dropdown like this, but only for Department a).

<input type="hidden" id="filtro_tipo" value="productos">
            <select name="filtro_marca" onchange="update_productos();" id="filtro_marca">
                <option value="0"><?php echo pll_current_language()=='es'?'Seleccione una marca':'Select a brand'; ?></option>
                <?php
                    $args = array(
                            'order'             => 'ASC',
                            'hide_empty'        => true,
                            'cache_domain'      => 'core'
                    );
                    $terms = get_terms('brand', $args);
                    foreach ($terms as $item) {
                        if ($item->count == 1) {
                            $tax_query = '';
                            $tax_query[] = array('taxonomy' => 'brand','field' => 'term_id','terms' => $item->term_id);
                            $term_post = get_posts(array('post_type' => 'prod','tax_query' => $tax_query));
                            if (!empty($term_post)) {
                                $term_post_link = get_permalink($term_post[0]->ID);
                                $id_prod = url_to_postid($term_post_link);
                                $nombre_prod = get_the_title($term_post[0]->ID);
                                echo '<option value="'.$nombre_prod.'" href="'.$id_prod.'">'.$item->name.'</option>';
                            }
                        } else {
                            echo '<option value="'.$item->term_id.'" label="'.$item->name.'">'.$item->name.'</option>';
                        }
                    }
                ?>
            </select>
0
, , German Gallo 5 years 2016-05-30T13:43:33-05:00 0 Answers 65 views 0

Leave an answer

Browse
Browse