Display 6 products in a random order from the latest 20 product posts
-
Hi dear supporters i really need this to be done, it’s an urgent
i want to Display latest 6 products in a row in a random order from the latest 20 product posts.
My points should be in the algorithm is…..
1. display 6 products in row, 2. from last 20 latest products, 3. in a random order. All these should be in samei want it from the last 20 latest products..
I got following code as a solution from a supporter but it shows very old products as well
function ijab_random_products() { ?> <ul class="products columns-3"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 20, 'orderby' => 'date', 'no_found_rows' => true, ); $counter = 1; $max = 6; $loop = new WP_Query( $args ); shuffle($loop->posts); if ( $loop->have_posts() ) { while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post(); wc_get_template_part( 'content', 'product' ); $counter++; endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?> </ul> <?php } add_shortcode( 'randomprod', 'ijab_random_products' );
Can we do like this, the logic..
First we sort products by the date,
Second take 20 products starting from the first row
Third shuffle that 20 products we got and display 6 in a rowCan we do this and get this done
Can we change the above code something like this code please? i want to display 6 products from the latest 20 products in a random order (shuffle).. like this https://ibb.co/3cxn43C
Hope this code might give an idea..
$recent_posts = wp_get_recent_posts( array( ‘numberposts’ => 20, // Number of recent posts ‘post_status’ => ‘publish’, // Show only the published posts ‘post_type’ => ‘product’ )); $sub = array_splice( $recent_posts, 10, 10 );// array_splice ( array, offset, length ) shuffle( $sub ); // Random array_splice( $recent_posts, 10, 0, $sub ); foreach( $recent_posts as $post ) { echo $post[‘ID’] . ‘<br>’; //echo ‘ ', print_r( $post, 1), ' ‘; } // Loop
>> Show 6 products in a row from the last 20 products in a random order << i want it to be show like this as in the image https://ibb.co/3cxn43C
Thank you in advance dear supporters
- The topic ‘Display 6 products in a random order from the latest 20 product posts’ is closed to new replies.