WordPress.org

Ready to get started?Download WordPress

Forums

Display random post by cat name (3 posts)

  1. kenblaue
    Member
    Posted 6 years ago #

    Hi,
    I'm use this query I found on here on the WP forums.


    <?php
    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results("SELECT * 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_name = 'Designers we ♥' ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post):
    setup_postdata($post);
    ?>

    It's worked very well for me but I'm in the process of upgrading my theme to WP 2.5 and this is now the only thing broken. I know there have been changes to the WP Database Schema and I've tried to decipher it, with no luck at all. Can anyone help with this, or see something similar I can then work from.

    Thanks...

  2. willdayble
    Member
    Posted 6 years ago #

    use the force?

  3. desertmagic
    Member
    Posted 5 years ago #

    http://codex.wordpress.org/Template_Tags/get_posts#Random_posts

    For those too lazy to follow the link:

    <ul><li><h2>A random selection of my writing</h2>
        <ul>
     <?php
     $rand_posts = get_posts('numberposts=5&orderby=rand');
     foreach( $rand_posts as $post ) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
        </ul>
     </li></ul>

Topic Closed

This topic has been closed to new replies.

About this Topic