related products on woocommerce

i have next loop:

<?php woocommerce_product_loop_start(); ?>

<?php
        // Setup your custom query
$args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php woocommerce_get_template_part( 'content', 'product' ); ?>

<?php endwhile; // end of the loop. ?>

<?php woocommerce_product_loop_end(); ?>

From some reason that’s display always the same 4 products. what i want is – that on every refresh it will display differents products from all categories i have on WooCommerce.

Read More

What i need to add to the loop, or should i need to create something else for that?

Related posts

Leave a Reply

1 comment

  1. it might be a conflict with plugin. Some plugins disable ability for random ordering unless you filter it out with: remove_all_filters('posts_orderby');.

    So try to put that before your query:

    <?php
    
    remove_all_filters('posts_orderby');
    
    // Setup your custom query
    $args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
    $loop = new WP_Query( $args );