• Hi,

    I have set up a custom select query using the following codex doc and it works well – it uses setup_postdata($post) to get posts:
    http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    One thing I would like to do is display only one random post, but I have tried
    <?php query_posts('numberposts=1&orderby=rand'); ?>
    and

    <?php
     $rand_posts = get_posts('numberposts=1&orderby=rand');
     foreach( $rand_posts as $post ) :
     ?>

    but neither seem to do the trick.
    Any ideas how to incorporate this functionality into the code:

    <?php if ($pageposts): ?>
     <?php foreach ($pageposts as $post): ?>
     <?php setup_postdata($post); ?>

    Thanks,

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php
    $rand_posts = get_posts('showposts=1&orderby=rand');
    echo "<pre>"; print_r($rand_posts); echo "</pre>";
    ?>

    Works fine for me.

    Now just need a loop to display those posts. Once you read about The Loop, then that should also answer your 2nd question.

    Thread Starter janieavis

    (@janieavis)

    Thanks Michael! but I am not sure how to integrate this into the above code because this functionality does not seem to use the standard loop.

    My understanding about this functionality is that a certain group of posts are selected and put in $posts & then it uses setup_postdata instead of the regular get_posts function to display them.

    <?php if ($pageposts): ?>
     <?php foreach ($pageposts as $post): ?>
     <?php setup_postdata($post); ?>

    If I tack your code onto the end of this code it displays any random post from the entire database but I am trying to get a random post only from those contained in $post.

    Do you have any ideas how to make this work?
    Thanks! janie

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘random posts for custom select query?’ is closed to new replies.