Forum Replies Created

Viewing 1 replies (of 1 total)
  • It looks like the post_category is not used in the posts table anymore (if it ever did). instead you need to join with the post2cat table. The code below grabs 2 random posts from category 2.

    <?php
    global $wpdb;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts AS p, $wpdb->post2cat AS c WHERE p.ID = c.post_id AND c.category_id = 2 AND p.post_status = 'publish' ORDER BY RAND() LIMIT 2");
    foreach ($rand_posts as $post) :
    setup_postdata($post);
    ?>

Viewing 1 replies (of 1 total)