WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] help on how to randomize posts (13 posts)

  1. elyonline
    Member
    Posted 4 years ago #

    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

    '
    <?php
    // This file displays the latest video article on the front page
    rewind_posts();
    // 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">
    <div>
    <?php
    // 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" />
    </object>

    <?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>
    </div> <!-- video -->
    '

  2. vtxyzzy
    Member
    Posted 4 years ago #

    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'.

  3. elyonline
    Member
    Posted 4 years ago #

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

    Thanks anyway

  4. vtxyzzy
    Member
    Posted 4 years ago #

    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.

  5. alism
    Member
    Posted 4 years ago #

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

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

  6. vtxyzzy
    Member
    Posted 4 years ago #

    Or try adding 'caller_get_posts=1' to the query parameters.

  7. elyonline
    Member
    Posted 4 years ago #

    @ vtxyzzy

    Tried:

    '$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?

  8. vtxyzzy
    Member
    Posted 4 years ago #

    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;`.

  9. Shane G.
    Member
    Posted 4 years ago #

    Hi,

    Check with this plugin:

    http://wordpress.org/extend/plugins/random-featured-post-plugin/

    Thanks,

    Shane G.

  10. elyonline
    Member
    Posted 4 years ago #

    @ vtxyzzy

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

    Here is my code now: http://paste-it.net/public/d0e48e3/

    Not sure what other options I can take.

  11. vtxyzzy
    Member
    Posted 4 years ago #

    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.

  12. elyonline
    Member
    Posted 4 years ago #

    @ 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 :)

  13. vtxyzzy
    Member
    Posted 4 years ago #

    You are most welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags