title: Wordpress / WooCommerce - Related Products by Attribute
body: <p>I know I'm not the first and I probably won't be the last to try to make this journey work. I've looked at everything that's out there.</p>
  
  <p>Most answers are pre WC 2.1.</p>
  
  <p>A lot of the replies work with the tax_query. Every once in a while, someone tries to touch the query. I've tried both, neither option works for me.</p>
  
  <p><strong>How do I adjust the Related Products shown in WooCommerce to include relationships via Custom Attributes?</strong></p>
  
  <p><strong>Goal</strong>: Relationship via Cat AND (Brand OR Artist OR Manufacturer)</p>
  
  <p>Test via <code>woocommerce_product_related_posts_query</code>:</p>
  
  <pre><code>function product_related_posts_relate_by_attributes ( $query ){
      global $product;
  
      //*
      $brands_array        = wc_get_product_terms( $product-&gt;id, 'pa_brand', array( 'fields' =&gt; 'ids' ) );
      $artists_array       = wc_get_product_terms( $product-&gt;id, 'pa_artist', array( 'fields' =&gt; 'ids' ) );   
      $manufacturers_array = wc_get_product_terms( $product-&gt;id, 'pa_manufacturer', array( 'fields' =&gt; 'ids' ) );
  
      $query['where'] .= ' AND (';        
          $query['where'] .= " ( tt.taxonomy = 'pa_brand' AND t.term_id IN ( " . implode( ',', $brands_array ) . " ) ) ";     
          $query['where'] .= ' OR ';  
          $query['where'] .= " ( tt.taxonomy = 'pa_artist' AND t.term_id IN ( " . implode( ',', $artists_array ) . " ) ) ";       
          $query['where'] .= ' OR ';  
          $query['where'] .= " ( tt.taxonomy = 'pa_manufacturer' AND t.term_id IN  ( " . implode( ',', $manufacturers_array ) . " ) ) ";      
      $query['where'] .= ')';//*/
  
      return $query;
  }
  add_filter('woocommerce_product_related_posts_query', 'product_related_posts_relate_by_attributes');
  </code></pre>
  
  <p>Test via <code>woocommerce_related_products_args</code>:</p>
  
  <pre><code>function custom_related_product_args ( $args ){
      global $product;
  
      $cats          = wc_get_product_terms( $product-&gt;id, 'product_cat', array( 'fields' =&gt; 'slug' ) );
      $brands        = wc_get_product_terms( $product-&gt;id, 'pa_brand', array( 'fields' =&gt; 'slug' ) );
      $artists       = wc_get_product_terms( $product-&gt;id, 'pa_artist', array( 'fields' =&gt; 'slug' ) );    
      $manufacturers = wc_get_product_terms( $product-&gt;id, 'pa_manufacturer', array( 'fields' =&gt; 'slug' ) );
  
      unset( $args['post__in'] );
      $args['tax_query'] = array( 
          'relation' =&gt; 'AND',
          array(
              'taxonomy' =&gt; 'category',
              'field'    =&gt; 'slug',
              'terms'    =&gt; $cats,
          ),
          array(
              'relation' =&gt; 'OR',
              array(
                  'taxonomy' =&gt; 'pa_brand',
                  'field'    =&gt; 'slug',
                  'terms'    =&gt; $brands,
              ),
              array(
                  'taxonomy' =&gt; 'pa_artist',
                  'field'    =&gt; 'slug',
                  'terms'    =&gt; $artists,
              ),
              array(
                  'taxonomy' =&gt; 'pa_manufacturer',
                  'field'    =&gt; 'slug',
                  'terms'    =&gt; $manufacturers,
              )
          )
      );
  
      return $args;
  }
  add_filter('woocommerce_related_products_args', 'custom_related_product_args');
  </code></pre>
  
  <p>Could someone please be so gracious as to tell me where I'm going wrong?</p>
  
  <p>Thank you very much in advance!</p>