Support » Fixing WordPress » How to create a page with post from random category

Viewing 1 replies (of 1 total)
  • If you already know how to create a page of posts, you can use this code to select a random category ID to use in the query:

    // Select a random category ID
       $sql = "SELECT DISTINCT tr.term_taxonomy_id
          FROM $wpdb->term_relationships tr
          JOIN $wpdb->posts p ON (p.ID = tr.object_id)
          JOIN $wpdb->term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id)
          WHERE p.post_type = 'post'
          AND p.post_status = 'publish'
          AND tt.taxonomy = 'category'
          AND tt.term_taxonomy_id <> 1
          ORDER BY RAND()";
       $cat = $wpdb->get_var($sql);
       echo "CAT = $cat<br />";  // Use this for testing
Viewing 1 replies (of 1 total)
  • The topic ‘How to create a page with post from random category’ is closed to new replies.