I am trying to write some custom SQL to return all the posts tagged with 'x'. I am aware that this is easy to achieve with the codex but this is only the start of my challenge because once this query is working I need to add on some additional data from another table to order them by highest page views.
I already have this SQL written and working for...
- The most viewed posts in the blog
- The most viewed posts in category 'x'
Now I am struggling to get it working for
- The most viewed posts tagged with 'x'
So far I have got...
SELECT * FROM wp_posts
LEFT JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_ID
LEFT JOIN wp_terms
ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id
WHERE wp_terms.name = 'x'
This is sort of working as it returns a single post which is indeed tagged with 'x'. However it should be returning four posts tagged with 'x'. Anyone have any ideas where I am going wrong?
Cheers