Alex Rivera | Logout

How to select posts with specific tags/categories in WordPress

Asked 2008-08-26T14:29:38.093
10

This is a very specific question regarding MySQL as implemented in WordPress.

I'm trying to develop a plugin that will show (select) posts that have specific 'tags' and belong to specific 'categories' (both multiple)

I was told it's impossible because of the way categories and tags are stored:

  1. wp_posts contains a list of posts, each post have an "ID"
  2. wp_terms contains a list of terms (both categories and tags). Each term has a TERM_ID
  3. wp_term_taxonomy has a list of terms with their TERM_IDs and has a Taxonomy definition for each one of those (either a Category or a Tag)
  4. wp_term_relationships has associations between terms and posts

How can I join the tables to get all posts with tags "Nuclear" and "Deals" that also belong to the category "Category1"?

Edit
Report

1 Answer

0

Thanks @Eric it works! Just a few code corrections for future reference:

  • the first select statements misses a coma after wp_term_relationship tr2
  • In the same select statemt the following must be change:
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

tr2

should be

wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

tr3
answered 2008-08-28T11:22:40.257

Your Answer