A
A
Alexander Bondarenko2021-06-22 21:10:16
WordPress
Alexander Bondarenko, 2021-06-22 21:10:16

How to get related products of a WooCommerce product?

How can I get related products and get related products by category id?
It turned out to be the only way to do it, but I don’t know how to sort and get by id.
Thanks in advance!

$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'posts_per_page'        => '100',
    'tax_query'             => array(
         array(
             'taxonomy'  => 'product_cat',
             'field'     => 'term_id',
             'terms'     => array(33,25),
             'operator'  => 'IN',
         ),
         ),
 );
 
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
    echo '<li><a href="'. get_permalink() .'"><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></a></li>';
endwhile;

wp_reset_postdata();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question