How to select posts with specific tags/categories in WordPress How to select posts with specific tags/categories in WordPress wordpress wordpress

How to select posts with specific tags/categories in WordPress


I misunderstood you. I thought you wanted Nuclear or Deals. The below should give you only Nuclear and Deals.

select p.*from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_idand p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_idand p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_idand (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')


What a gross DB structure.

Anyway, I'd do something like this (note I prefer EXISTS to joins, but you can re-write them as joins if you like; most query analyzers will collapse them to the same query plan anyway). You may have to do some additional juggling one way or another to make it work...

SELECT *  FROM wp_posts p WHERE EXISTS( SELECT *                 FROM wp_term_relationship tr                WHERE tr.object_id = p.id                  AND EXISTS( SELECT *                                FROM wp_term_taxonomy tt                               WHERE tt.term_taxonomy_id = tr.term_taxonomy_id                                 AND tt.taxonomy         = 'category'                                 AND EXISTS( SELECT *                                               FROM wp_terms t                                              WHERE t.term_id = tt.term_id                                                AND t.name    = "Category1"                                            )                            )                  AND EXISTS( SELECT *                                FROM wp_term_taxonomy tt                               WHERE tt.term_taxonomy_id = tr.term_taxonomy_id                                 AND tt.taxonomy         = 'post_tag'                                 AND EXISTS( SELECT *                                               FROM wp_terms t                                              WHERE t.term_id = tt.term_id                                                AND t.name    = "Nuclear"                                            )                                 AND EXISTS( SELECT *                                               FROM wp_terms t                                              WHERE t.term_id = tt.term_id                                                AND t.name    = "Deals"                                            )                            )            )


Try this:

select p.*from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship trwp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2where p.id = tr.object_idand t.term_id = tt.term_idand tr.term_taxonomy_id = tt.term_taxonomy_idand p.id = tr2.object_idand t2.term_id = tt2.term_idand tr2.term_taxonomy_id = tt2.term_taxonomy_idand (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))

Essentially I'm employing 2 copies of the pertinent child tables - terms, term_taxonomy, and term_relationship. One copy applies the 'Category1' restriction, the other the 'Nuclear' or 'Deals' restriction.

BTW, what kind of project is this with posts all about nuclear deals? You trying to get us on some government list? ;)