WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] I'm trying to display a random post (6 posts)

  1. jugularbean
    Member
    Posted 7 years ago #

    So I need to display a random post, but in a seperate loop. So I'm using the following code

    global $wpdb;
    q = "SELECT COUNT(*) FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_ID = 5";
    $s = $wpdb->get_var($q);
    $intOffset = rand(1,intval($s));
    echo $s . "<br/>" . $intOffset;
    $my_query = new WP_Query('cat=5&showposts=1&offset=$intOffset');
    if ($my_query->have_posts()) :
    while ($my_query->have_posts()) : $my_query->the_post();

    The value of $intOffset is displaying a random variable between the limits specified, but for some reason it's not working when I pass it as an offset value cause the latest post is the only displayed.

  2. jugularbean
    Member
    Posted 7 years ago #

    Nevermind, I've figured it out.

  3. vjsuperlight
    Member
    Posted 7 years ago #

    Hi jugularbean, would you be willing to post your fixed code here, i have been struggling trying to get this same function on my site. i've tried modifiying random post listing code to make it show content, but with no luck. you seem to have solved it perhaps?
    i'm not a programmer but i'm familiar with wordpress coding enough to get along.

    cheers

  4. whooami
    Member
    Posted 7 years ago #

    shuffle(query_posts("all your other crap in here"));

  5. vjsuperlight
    Member
    Posted 7 years ago #

    hi whooami, that's just a bit too brief for my programming skills, could you elaborate on your post please? is shuffle a built-in wordpress code? thanks

  6. whooami
    Member
    Posted 7 years ago #

    no, shuffle is a built-in PHP function

Topic Closed

This topic has been closed to new replies.

About this Topic