'Woocommerce custom loop with related taxonomies

I have a problem that I can't solve by myself, so I hope for a hand. I am developing a woocommerce theme, in which each product, a specific author, is associated with a custom taxonomy. Now I'm trying to create a while loop, which calls me all the products that have 2 or more related authors. To achieve this I created the following code:

    <?php 
$terms = $terms = get_terms([
  'taxonomy' => 'autore',
  'hide_empty' => true,
]);

$termsArray = array();
foreach( $terms as $term) {
  $slug = ' "'.$term -> slug . '", ';
  array_push( $termsArray, $slug );
}
$authorSlug = implode( $termsArray);

var_dump($authorSlug);


$custom_loop = new WP_Query( array(
  'post_type'      => 'product',
  'posts_per_page'=> -1,
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'tax_query' => array(
      array( 'taxonomy' => 'autore',
        'field'    => 'slug',
        'terms'    => array($authorSlug),
      )
   )
));

if ($custom_loop->have_posts()) : while($custom_loop->have_posts()) : $custom_loop->the_post(); 

  the_post_thumbnail('thumbnail', array('class' => '','alt' => get_the_title()));
  the_title();
     wp_reset_postdata(); 
 endwhile; endif;

?>

However the loop doesn't show me anything. The weird thing I can't figure out is that, by doing a var_dump (), it seems like everything seems to work. Is anyone able to help me understand where I'm wrong?



Solution 1:[1]

I found a question similar to mine, from the answer given, I changed my code like this, and now everything works. I am attaching it, maybe it will be useful to someone in the future.

<?php 
$terms = $terms = get_terms([
  'taxonomy' => 'autore',
  'hide_empty' => true,
]);

$termsArray = array();
foreach( $terms as $term) {
  $ids =  $term -> term_id . ' ';
  array_push( $termsArray, $ids );
}
$authorID = implode( $termsArray);
var_dump($authorID);



$custom_loop = new WP_Query( array(
  'post_type'      => 'product',
  'posts_per_page'=> -1,
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'tax_query' => array(
      array( 'taxonomy' => 'autore',
        'field'    => 'term_id',
        'terms'    => $authorID,
        'include_children' => true,
        'operator' => 'IN',
      )
   )
));

if ($custom_loop->have_posts()) : while($custom_loop->have_posts()) : $custom_loop->the_post(); 

  the_post_thumbnail('thumbnail', array('class' => '','alt' => get_the_title()));
  the_title();
     wp_reset_postdata(); 
 endwhile; endif;

?>

I simply moved the focus from the slug to the taxonomy ID.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eliolas