user147
user147

Reputation: 1310

Wordpress SQL statement help needed?

I want to select with sql latest post from one category,I know how to select latest post from all category but dont know how to JOIN tables.Can someone help me with this,and explain a little. My sql statement.

SELECT id,post_title,post_date FROM wp_posts ORDER BY post_date ASC

How to select from one category,I'm trying some code,looking INNER JOIN examples,but it doesnt work,help please.

Upvotes: 0

Views: 212

Answers (1)

Chris Cameron-Mills
Chris Cameron-Mills

Reputation: 4657

Going by WP ERD

I'd say you want to join through to wp_terms using appropriate join conditions and specify which term you want in your WHERE clause.

SELECT
  p.*
FROM wp_posts p
JOIN wp_term_relationships wtr ON p.id = wtr.object_id
JOIN wp_term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_id
JOIN wp_terms wt ON wtt.term_id = wtt.term_id
WHERE wtt.name = 'Some Term'

You may need further restrictions on the join/where.

Upvotes: 5

Related Questions