[Solved] Get posts that matches specific terms of multiple custom taxonomies


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

solved Get posts that matches specific terms of multiple custom taxonomies