WordPress.org

Ready to get started?Download WordPress

Forums

Fetch Random Post in WP 2.0.11 Possible? (5 posts)

  1. Joni
    Member
    Posted 5 years ago #

    I'm still using 2.0.11 (for reasons I don't feel like going into here). I have several query strings on my front page (index.php) that pull posts from various categories.

    I wanted to create a box that would pull random posts from a certain category. I was so happy to remember this:

    <!-- DISPLAY RANDOM QUOTE -->
    <div id="quotes">
    <h2 class="headline">Someone Else Said it Better</h2>
    <?php query_posts('cat=47&showposts=2&orderby=rand'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p class="rand-quote-text" id="post-<?php the_ID(); ?>"><?php the_title(); ?>
    
    <?php the_content(); ?>
    <?php endwhile; else: ?>
    <?php endif; ?>
    </div><!-- /quotes -->

    But it dawns on me that this code only works for WP 2.5.x and higher. Do I have to use a plugin to do what I want here? Right now, I have a "Random Quotes" category, category ID 47. The page title is the quote itself and the body (content) is the author name. I think that's easier than a plugin.

    Any hope for me? Thanks!

  2. Joni
    Member
    Posted 5 years ago #

    Hmm. Think this might work. I'll let you guys know.
    http://codex.wordpress.org/Template_Tags/get_posts

  3. Joni
    Member
    Posted 5 years ago #

    Nope. This code:

    <!-- DISPLAY RANDOM QUOTE -->
    <div id="quotes">
    <h2 class="headline">Someone Else Said it Better</h2>
    <?php
    $rand_posts = get_posts('numberposts=1&category=47&orderby=rand');
    foreach( $rand_posts as $post ) :
    ?>
    <p class="rand-quote-text"><?php the_title(); ?>
    <?php endforeach; ?>
    <?php the_content(); ?>
    </div><!-- /quotes -->

    throws out this error message and an odd post, not the latest one and not the one in category ID 47:

    WordPress database error: [Unknown column 'rand' in 'order clause']
    SELECT DISTINCT * FROM site_posts , site_post2cat WHERE post_date <= '2008-09-02 12:30:11' AND (post_status = 'publish') AND site_posts.ID = site_post2cat.post_id AND site_post2cat.category_id = 47 GROUP BY site_posts.ID ORDER BY rand DESC LIMIT 0,1

    I'm off in search of a plugin...

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    orderby=rand was not available in earlier versions of WordPress.

    You might try "orderby=RAND()" instead. That *might* work.

  5. Joni
    Member
    Posted 5 years ago #

    Nah, that didn't work, but I did find a plugin (Sobek's Posts in Categories) that might do the trick. I've got this site kinda sorta ported to WP 2.5.x .. but there's some stuff that doesn't work (Mini Blog) and I'm not willing to give that up just yet! :)

    Thanks, Otto.

    Update: That didn't work either; it requires at least 2.3. So I'm just going to keep the status quo, and when I want to change out the quotation, I'll just make the others a draft and make the one I want published. It will work for now. :)

Topic Closed

This topic has been closed to new replies.

About this Topic