Forums

orderby=rand doesn't work ? (12 posts)

  1. visio159
    Member
    Posted 3 years ago #

    The code given in codex for tag get_posts() displaying random posts doesn't work. It displays the post as if rand is non-existant.

    <ul><li><h2>A random selection of my writing</h2>
        <ul>
     <?php
     $rand_posts = get_posts('numberposts=5&orderby=rand');
     foreach( $rand_posts as $post ) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
        </ul>
     </li></ul>

    Any ideas ?

  2. educationthemes
    Member
    Posted 3 years ago #

    I am using the following code:

    <?php $recent = new WP_Query("cat=4&showposts=1&orderby=rand"); while($recent->have_posts()) : $recent->the_post(); ?>
    
    //do stuff
    
    <?php endwhile; ?>

    This is working for me dislaying a random post for category 4. You can see it in action on http://www.equaldesign.co.uk in the top right corner bringing in different random testimonials (not on the home page).

    I have had hit and miss with the random function though and had to trawl through a number of ways of doing it. The above is working at the moment.

  3. visio159
    Member
    Posted 3 years ago #

    Hi thanks for the help but it didn't work for me, I had to use wpdb to query(actually subquery) database directly and get 5 random posts.

    Here is the SOLUTION: Just change the numeric value after limit to any number and the the no. of posts displayed will be the equal to that number.

    <?php
    
     $querystr = "SELECT * FROM (SELECT wposts.* FROM $wpdb->posts wposts WHERE wposts.post_status = 'publish' ORDER BY rand()) as y limit 5 ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php //setup_postdata($post); ?>
    
          <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    
        <?php endforeach; ?>
    
      <?php else : ?>
        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>
     <?php endif; ?>
  4. vernal
    Member
    Posted 3 years ago #

    The post by educationthemes worked for me.

  5. sinha
    Member
    Posted 3 years ago #

    I am running this query
    <?php $featured = new WP_query("cat=11&showposts=1&orderby=rand"); ?>
    and somehow it fetches only the first post (in that category).

    Any idea how to resolve this?
    PS: I am running the latest wp version

  6. Len
    Member
    Posted 3 years ago #

    Look more closely at what you posted. ;)

    showposts=1

  7. sinha
    Member
    Posted 3 years ago #

    Lenk
    Sorry for the stupidity - but I stil don't quite get it.
    Req you to help me out :)
    <?php $featured = new WP_query("cat=2&showposts=1&orderby=rand"); ?>

    What's wrong with this?

  8. Len
    Member
    Posted 3 years ago #

    Hi sinha. You're not being stupid. Many times, after having spent several hours working on some code I overlook something obvious. :)

    You say this snippet of code only fetches the first post. You're using the showpost= argument in your query but look at how you're using it...

    showposts=1

  9. sinha
    Member
    Posted 3 years ago #

    Umm..but showposts is meant to fetch 'n' number of queries (link)
    What's the right code then?

  10. Len
    Member
    Posted 3 years ago #

    Perhaps I'm not understanding what you're asking. If you want to display more than one post then adjust showposts= accordingly.

  11. sinha
    Member
    Posted 3 years ago #

    Basically, I do want to display only 1 post - but randomly from a selected category.
    THe query that I am running fetches the first post - so essentially, rand function isn't working fine.

    And I am running 2.6.5 - any help?

  12. Len
    Member
    Posted 3 years ago #

    Hmmm, try this...

    <?php $featured = new WP_Query("cat=2&showposts=1&orderby=rand"); ?>
    <?php if ($featured->have_posts()) :  ?>
    <?php while ($featured->have_posts()) : $featured->the_post(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic