title: WooCommerce - Filtering related products by product attribute and category
body: <p>I'm looking to adapt the related products so that it returns products that match the colour and category of the current product.</p>
  <p>I can get one or the other working, but not both.</p>
  <p>Please see code below:</p>
  <pre><code>$attribute   = 'pa_colour';
  
  $term_slugs = wp_get_post_terms( $product_id, $attribute, ['fields' =&gt; 'slugs'] );
  
  $terms = get_the_terms( $post-&gt;ID, 'product_cat' );
  foreach ($terms as $term) {
      $product_cat_id = $term-&gt;term_id;
      break;
  }
  
  if ( empty($term_slugs) )
      return $related_posts;
  
  $new_relations = get_posts( array(
      'post_type'            =&gt; 'product',
      'posts_per_page'       =&gt; 4,
      'post__not_in'         =&gt; array( $product_id ),
      'tax_query'            =&gt; array( 
          'relation' =&gt; 'AND',
          array(
              'taxonomy' =&gt; $attribute,
              'field'    =&gt; 'slug',
              'terms'    =&gt; $term_slugs,
          ),
          array(
              'taxonomy' =&gt; 'product_cat',
              'field'    =&gt; 'slug',
              'terms'    =&gt; $product_cat_id,
          ),
      ),
      'fields'  =&gt; 'ids',
      'orderby' =&gt; 'rand',
  ) );
  </code></pre>
  <p>If anyone can point me in the right direction, that would be great.</p>
  <p>Thanks</p>