WordPress.org

Ready to get started?Download WordPress

Forums

Random post from category (6 posts)

  1. blogjunkie
    Member
    Posted 6 years ago #

    Hi,

    I want to select 6 random posts from a specific category and display them in my template. I've seen this thread, but the code doesn't work for WP2.3 because post2cat has been deprecated.

    Please, I really need a solution. Thanks in advance.

  2. MichaelH
    Member
    Posted 6 years ago #

    Using Otto's magic I didn't test this but try it (it's testing for category=4):

    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON
    ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON
    ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id = 4
    ORDER BY RAND()
    LIMIT $numposts
  3. Kafkaesqui

    Posted 6 years ago #

    Or:

    http://wordpress.org/support/topic/70359#post-444323

    (which is the only thing I'm pointing out for random posts now...)

  4. blogjunkie
    Member
    Posted 6 years ago #

    Thanks guys, this is awesome :o)

  5. c-bass
    Member
    Posted 6 years ago #

    how do i put this code in the loop? i ve tried:

    <?php // $the_query = new WP_Query('cat=-' .$catid. ',-' .$catid2. '&showposts=1&orderby=post_date&order=desc');
    				$the_query = "SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON
    ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON
    ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id = 4
    ORDER BY RAND()
    LIMIT $numposts";
    
    				while ($the_query->have_posts()) : $the_query->the_post();
    
    				$do_not_duplicate = $post->ID; ?>

    but it doesnet work.

  6. dynexd
    Member
    Posted 6 years ago #

    Actually, you have to use query_posts with orderby=random and cat=

    http://codex.wordpress.org/Template_Tags/query_posts

Topic Closed

This topic has been closed to new replies.

About this Topic