WordPresss query to fetch the posts under the given categories

<?php
 global $wpdb;
 $const="4,8,7"; //category Ids
 $querystr = "SELECT distinct(wposts.post_title),wposts.ID  FROM $wpdb->posts
 wposts LEFT JOIN $wpdb->term_relationships ON (wposts.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->term_taxonomy.taxonomy
 = 'category'  AND $wpdb->term_taxonomy.term_id IN($const) ORDER BY rand() ";
 $pageposts = $wpdb->get_results($querystr, OBJECT);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *