WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Randomise result of new wp_query (10 posts)

  1. richarduk
    Member
    Posted 3 years ago #

    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 Loop
  2. vtxyzzy
    Member
    Posted 3 years ago #

    This 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
       }
    ?>
  3. richarduk
    Member
    Posted 3 years ago #

    Cool. I'll have a look at that. Couldn't I shuffle the array?

  4. vtxyzzy
    Member
    Posted 3 years ago #

    That is what I did. Although it is not truly random, and the shuffle function may be more random.

  5. richarduk
    Member
    Posted 3 years ago #

    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.

    :-(

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    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 );
  7. richarduk
    Member
    Posted 3 years ago #

    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();?>
  8. vtxyzzy
    Member
    Posted 3 years ago #

    Glad you got it working! Now, please use the dropdown at top right to mark this topic 'Resolved'.

  9. Mark / t31os
    Moderator
    Posted 3 years ago #

    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();
    ?>
  10. jmh
    Member
    Posted 3 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic