Displaying entries on a CPT archive page in random order in WordPress

Posted on Leave a comment
5/5 - (355 votes)

Jami asks,

Looking for help on where to search for editing Minimum Pro’s Portfolio Archive order… I’d love for it to be done in a random order. I’m familiar with using custom fields to order them alphabetically (having done this for a directory style version of the portfolio archive on another site), but this time I’m coming up empty handed in google while trying to find answers for setting the order to be random.
Thanks for your help!

This can be done using pre_get_posts as explained by Bill Erickson here.

Step 1

Add the following in child theme’s functions.php:

add_action( 'pre_get_posts', 'sk_portfolio_archive_random' );
 * Set Random order for entries in Portfolio archive page
 * @author Bill Erickson
 * @author Sridhar Katakam
 * @link http://www.billerickson.net/customize-the-wordpress-query/
 * @param object $query data
function sk_portfolio_archive_random( $query ) {

	if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) {
		$query->set( 'orderby', 'rand' );


Step 2

Regenerate .htaccess by going to Settings > Permalinks and clicking ‘Save Changes’.

After this, on each Portfolio archive page load entries appear in random order vs the default latest first (DESC).

Replace portfolio with your CPT’s Post Type Key.

Leave a Reply

Your email address will not be published. Required fields are marked *