-2

I'm working on a page that show a lot of products. My structure is

  • 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)

Each product always has to have one brand AND one department. However, there may be a few products in each brand.

So, I need to do a list that shows me the all the products that matches both a specific brand AND a specific department, and also that let me print the name of the brand. i.e: In department A we have 3 products that matches Brand A.

Is there any way to do it? I'm able to filter either one a brand or a department, but not both at the same time.

Thanks!

EDIT:

This is my code to get all the products of a specific department, and it's working great.

$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(); ?>         

     <a  rel="<?php the_ID(); ?>" title="<?php the_title(); ?>" class="producto btn btn-raised cardio-dark"><?php the_title(); ?></a>
<?php
endwhile;

wp_reset_query();
} ?>

But what I need is to get the product's brand instead of product's name, and that if 2 or more products have the same brand, the brand appear only one time. So I can produce a list as the described above. i.e: Department A has 2 "Brand A" products and one "Brand B" product.

  • Would be easier to give a helpful answer - to what otherwise appears to be as simple logic/syntax question - if you showed the specific code you're using or have tried. – CK MacLeod May 29 '16 at 23:24
  • Hi CK MacLeod, I've just updated my post to explain a little more what my problem is. Thanks. – Germán Gallo May 29 '16 at 23:53
  • So, what I understand from your question is that you have some number of "products" (custom post type "prod") with custom terms under "Department" and "Brand." The main ask is to produce a list broken down so that the brands that exist in each Department are enumerated. So something like this, prior to formatting adjustments: "Department A" - "Brand 1" (1) - "Brand 2" (3) - "Brand 4" (1); "Department B" - "Brand 3" (2) - "Brand 4" (4) Etc. With the actual names within the quotes, and the numbers in parentheses tallying the quantities. Is that correct? – CK MacLeod May 30 '16 at 17:44
  • 3
    Hi CK; I have solved how to show which brands are in each department, but I have a problem of how to group the products that matches the same brand. I've posted and explained it better here – Germán Gallo May 30 '16 at 18:44
  • 1
    @TheDeadMedic It's not a duplicate, even if it looks that way. The OP was just lazy and copy/pasted the intro that matched both questions equally, so it looked like a dupe. In fact it's two different questions and I reopened the other one. – kaiser Jun 6 '16 at 21:19
0

I've finally solved this problem. I'm copying the code because it may help someone :) I've found the solution by populating an arrray and checking if a brand is or is not there, so I only get each brand one time.

   <?php

    //Query to match department
    $args = array(
     'tax_query' => array(
         array(
             'taxonomy' => 'department',
             'field' => 'slug',
             'terms' => array( 'department_name' )
         ),
     ),
     'orderby' => 'title',
     'order' => 'ASC',
     'post_type' => 'prod', 'posts_per_page' => '-1', 'posts_per_page' => '-1'
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {

      // Array of brands
      $brands= array();

      $term = $query->queried_object;

     while ( $query->have_posts() ) : $query->the_post(); ?>
         <!-- Loop to show the brand -->
         <?php
          $terms = get_the_terms( $post->ID , 'brabds' );
 // Loop por el array de marcas
          if ( $terms != null ){
            foreach( $terms as $term ) {

              // I want to check if my brand has only one product or more
              // Only one product
              if ($term ->count == 1){
                // I create a variable so I can check if it's in my brands array
                   $marca = $term->name;
                // If is not in array, push it there
                    if ( ! in_array($marca, $marcas)){
                        array_push($marcas, $marca);
                // This query is to link to a product and get some info
                        $tax_query = '';
                                    $tax_query[] = array('taxonomy' => 'brands','field' => 'term_id','terms' => $term->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 '<a title="'.$term->name.'" rel="'.$id_prod.'">'.$term->name.'</a>';
                                    }
                    }
              }
              // Marcas MULTI
              else{
                // If there are more than a product, do the same thing but query as my needs
                    $marca = $term->name;                    
                    if ( ! in_array($marca, $marcas)){
                        array_push($marcas, $marca);
                        echo '<a rel="'.$term->term_id.'" title="'.$term->name.'">'.$term->name.'</a>';
                    }
              }
            }
          }
          // End loop
?>

    <?php
    endwhile;

    wp_reset_query();
    } ?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.