Anyone any idea how I randomise the result of a new wp_query? I'm ordering posts by comment count, but want to randomise the end result
$newloop = new WP_Query('orderby=comment_count&posts_per_page=10');?>
Start LoopAnyone any idea how I randomise the result of a new wp_query? I'm ordering posts by comment count, but want to randomise the end result
$newloop = new WP_Query('orderby=comment_count&posts_per_page=10');?>
Start LoopThis may mot be the most elegant, but it should work:
<?php
$newloop = new WP_Query('posts_per_page=10');
// Get the posts out of the query object into an array
foreach ($newloop->posts as $apost) {
$posts[] = $apost;
}
// Randomize the array
for ($i=0;$i < sizeof($posts);++$i) {
$j = rand(0,sizeof($posts) - 1);
$temp = $posts[$i];
$posts[$i] = $posts[$j];
$posts[$j] = $temp;
}
// Process the array
global $post;
foreach ($posts as $post) {
setup_postdata($post);
// Process one post
}
?>Cool. I'll have a look at that. Couldn't I shuffle the array?
That is what I did. Although it is not truly random, and the shuffle function may be more random.
Well, your code works but it adds the number of posts specified in the wp_query onto the number of posts specified from within the Admin Panel.
:-(
I'd have thought a shuffle is sufficient myself..
$newloop = new WP_Query;
$newloop->query( 'orderby=comment_count&posts_per_page=10' );
shuffle( $newloop->posts );Done it.
It was a clash of variable names. I changed $post to $postblah
<?php // How many posts to show, most commented first. caller_get_posts=1 prevents sticky posts from being at the top
$loop_most_popular = new WP_Query('orderby=comment_count&posts_per_page=6&caller_get_posts=1');?>
<?php
// Get the posts out of the query object into an array
foreach ($loop_most_popular->posts as $apost) {
$postsblah [] = $apost;
}
// Randomize the array
shuffle ($postsblah);
// Process the array
global $post; ?>
<?php foreach ($postsblah as $post) : ?>
<?php setup_postdata($post); ?>
<li class="popular">
<a class="permalink" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?></a>
</li>
<?/*
Title of post
*/?>
</ul>
<?php endforeach; ?>
<?php wp_reset_query();?>Glad you got it working! Now, please use the dropdown at top right to mark this topic 'Resolved'.
The WP_Query class has methods for looping over the results, why write a foreach loop? I'm referring to what is commonly known as "The Loop" which is part of the WP_Query class ...
<?php
if( $loop_most_popular->have_posts() ) :
while( $loop_most_popular->have_posts() ) :
$loop_most_popular->the_post();
?>
<!-- Your HTML, loop stuff goes here, for example(see below) -->
<li class="popular"><a class="permalink" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?></a></li>
<?php
endwhile;
endif;
wp_reset_query();
?>the shuffle() worked perfectly for my scenario. Had seen this used for a regular query, didn't know how to make it work for WP_Query, cheers!
This topic has been closed to new replies.