How to run this SQL query of wp_terms database

Per comments, you basically have it – just GROUP BY $wpdb->term_relationships.object_id instead; also to be pernickety, you’d use standard (INNER) JOINs rather than LEFT ones:

SELECT $wpdb->term_relationships.object_id
FROM $wpdb->term_relationships
    JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
    JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id   
WHERE name IN (394, 396, 988, 666)  
GROUP BY $wpdb->term_relationships.object_id
ORDER BY $wpdb->term_relationships.term_taxonomy_id DESC

Leave a Comment