Skip to content

How to Show 6 Random Products Out of the 20 Most Recent

Recently, a customer had an unusual request. They wanted to show six random products. That’s not hard at all, but they wanted these six to come from the 20 most recently published. That’s a bit trickier. The built-in [product] shortcode in WooCommerce can show 6 random products, but it can’t set set the overall pool of products to be the 20 most recent.

Since we don’t have an off-the-shelf shortcode for this, we’ll need to cook one up. To start out, let’s grab the sample product loop from the WooCommerce documentation.

<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!–/.products–>

That will return a list of the 12 most recent products. By default wp_query returns objects in descending order so the latest items will be first. First we need to make a few changes to the arguments. We need a pool of twenty products, so we’ll change that. I’m also adding in the date as the orderby parameter so that’ll be clear in case we ever have to come back and edit this code.

	$args = array(
		'post_type'			=> 'product',
		'posts_per_page'	=> 20,
		'orderby'			=> 'date',
		'order'				=> 'DESC',
		'no_found_rows'		=> true,
	);

The kicker here is we only want to show six of these twenty products. We can add a couple of variables here to handle that. We’ll have one for the count and another one for the maximum number of products. We’ll increment the $counter variable at the end of each loop so that it’ll keep track of where we are in the list. From there, we can add them to the while loop so it only runs six times.

	$counter = 1;
	$max = 6;
	...
	while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post();
	...
		$counter++;
	endwhile;

The next part is getting the six products to be random ones from our pool of twenty. We can use PHP’s built-in shuffle function to reorder the posts. We don’t apply it to $loop directly since it has more data than just the product content. We need to add it to the posts array that’s nested inside this multidimensional array. That’ll look like this:

	$loop = new WP_Query( $args );
	shuffle($loop->posts);

We can wrap this up in a function and add it to a shortcode. Then all we’ll need to do is insert [randomproducts] into the content of a page/post and we’ll have a list of six random products out of our most recent twenty.

function ijab_random_products() {
	?>

<ul class="products columns-3">
	<?php
	$args = array(
		'post_type'			=> 'product',
		'posts_per_page'	=> 20,
		'orderby'			=> 'date',
		'order'				=> 'DESC',
		'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( 'randomproducts', 'ijab_random_products' );

That’ll display the products in two rows of three columns. You can change the columns-3 in the UL element in that snippet to a different number to adjust the columns. Have fun!

Leave a Reply

%d bloggers like this: