• Mike

    (@michael-copestake)


    I’m trying to set up a query to grab three random posts. I have a ‘slider’ with three slides each showing three posts, one is the latest three, the second is ordered by posts with the most comments, the third is a random selection of three posts.

    The first two work fine, but after over an hour or tweaking and trying to get the random one working I’m getting nowhere. Here is the code I have at the moment:

    $arguments = array( 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'rand', 'numberposts' => '3', 'ignore_sticky_posts' => true );
    
    $query = new WP_Query( $arguments );
    if ( $query->have_posts() ) {
    	$i = 0;
    	while ( $query->have_posts() && $i < 3  ) {
    		$query->the_post();
    		{{ THE POST CONTENTS }}
    		$i++;
    	}
    } else {
    	echo 'No Posts!';
    }
    wp_reset_postdata();

    What seems to be happening, is including ‘orderby’ => ‘rand’ just displays posts in reverse date order(i.e. the three oldest posts), rather than three random posts.

    Any help would be greatly appreciate, this is driving me crazy!

The topic ‘Problem with orderby=rand’ is closed to new replies.