WordPress SQL Query by Category / Conditions

I am modifying the plugin a bit to suit my needs and I need to modify this request to return the post id of only one category. I know it has something to do with INNER JOIN but cannot get the correct query.

Here is the original request

$query = "SELECT ID as PID FROM $wpdb->posts";
$results = $wpdb->get_results($querydetails,ARRAY_A);

      

EDIT: ok, I found a way to make the request

$query = "
SELECT ID as PID 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 = 3
";

      

+2


a source to share


1 answer


WordPress recommends using it query_posts()

wherever possible when retrieving posts from the database. You can try instead:



// $cat contains the category ID
$posts = query_posts('cat=' . $cat);

      

+1


a source







All Articles