Support » Fixing WordPress » help on how to randomize posts

  • Resolved elyonline


    Hey all,

    I know this is a big ask bust I am trying to workout how I can randomize the following code (from Justin Tadlock) I have. This code currently pulls a video based on a custom field from a post. What I would dearly love is for the script to randomize any posts that has the custom field. I have been searching high and low but as of yet have found no tutorial.

    Can anyone help please?

    Thank you

    // This file displays the latest video article on the front page
    // Query one post from “video” category
    $my_query = new WP_Query(‘category_name=video&showposts=1’);
    while ($my_query->have_posts()) : $my_query->the_post();
    // Get the “Video” custom field Key as an array (displays single Value in the video block)
    $video = get_post_custom_values($key = ‘video-value’);

    <h5>” title=”<?php the_title(); ?>”><?php the_title(); ?></h5>

    <div class=”<?php echo $i; ?>video”>
    // Check to see if custom field “Video” is set and if it has anything in the “Value” field.
    if(isset($video[0]) && strcmp($video[0],”)!= 0) {
    // Display valid XHTML player for YouTube, Google, MetaCafe, and other video sites
    // “echo $video[0];” displays the first item in the array for the custom field “Video”

    <object type=”application/x-shockwave-flash” data=”<?php echo $video[0]; ?>” style=”width: 290px; height: 242px; border: none; padding: 0; margin: 0;” id=”video-block-<?php echo $i; ?>”>
    <param name=”movie” value=”<?php echo $video[0]; ?>” />
    <param name=’wmode’ value=’transparent’ />
    <param name=”quality” value=”best” />
    <param name=”bgcolor” value=”transparent” />
    <param name=”FlashVars” value=”playerMode=embedded” />

    <?php } // endif

    // If there is no Value for the custom field Key “Video”
    else {

    // echo error checking to check custom field errors
    echo ‘Did not add a video URL to the custom field Key of “Video”‘;
    echo ‘<!– This user did not add the video URL to the correct custom field –>’;

    } // endelse
    </div> <!– video –>

Viewing 12 replies - 1 through 12 (of 12 total)
  • Try changing this:

    $my_query = new WP_Query('category_name=video&showposts=1');

    to this:

    $my_query = new WP_Query('category_name=video&showposts=1&orderby=rand');

    If this works, please mark this topic ‘Resolved’.

    Hmmm, that didn’t work, was convinced it would.

    Thanks anyway

    I wasn’t sure it would work with showposts=1. Try removing the showposts=1, and putting a break statement just before the endwhile that ends the while loop.

    Can you try that again with the wp-sticky plugin disabled?

    You must be just down the road from me by the way… spooky.

    Or try adding ‘caller_get_posts=1’ to the query parameters.

    @ vtxyzzy


    ‘$my_query = new WP_Query(‘category_name=video&caller_get_posts=1&caller_get_posts=1&orderby=rand’);’

    This just lists every ‘video’ post though. Not too sure what you mean’t by a break statement though.

    Again, thank you for your help.

    @ alism

    Where you from?

    The break statement breaks out of the endwhile loop, so only the first post will show.

    I can’t see the endwhile in the code you posted, so I can’t give you the exact coding. It should either be <?php break; ?> or just ‘break;`.


    Check with this plugin:


    Shane G.

    @ vtxyzzy

    Thank you again, alas, it did not work. Thank you for your time.

    Here is my code now:

    Not sure what other options I can take.

    You said it did not work, but didn’t say exactly what it did that was not what you wanted. Please explain.

    Looks like you took the ‘orderby=rand’ out of the query. Put it back in and try again.

    @ vtxyzzy

    My apologies! I just realised I missed out the &orderby=rand’

    All is working just dandy now. Thank you

    I really appreciate the help you have supplied 🙂

    You are most welcome!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘help on how to randomize posts’ is closed to new replies.