The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Display random post by cat name (3 posts)

  1. kenblaue
    Posted 8 years ago #

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

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

    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.


  2. willdayble
    Posted 8 years ago #

    use the force?

  3. desertmagic
    Posted 8 years ago #


    For those too lazy to follow the link:

    <ul><li><h2>A random selection of my writing</h2>
     $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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic