views:

43

answers:

1

Hello,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.

+5  A: 

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.

ChrisCM
I wish all answers be so illustrative like yours.
Nazariy
Wow nice graph,yes,I want to select only posts from one category where there status is publish order by date.Can you explain me on example how to do that?I'm looking this graph,it helps but it would be much easier if you can explain me on example how to do that.Tnx in advance.
Tnx CrhisCM I will study your code.
SELECT p.* FROM wp_posts p WHERE post_status='publish' ORDER BY post_dateJOIN wp_term_relationships wtr ON p.id = wtr.object_idJOIN wp_term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_idJOIN wp_terms wt ON wtt.term_id = wt.term_idWHERE wtt.name = 'my-cat'It doesnt work this for me,why?
Here is code that works : SELECT * FROM wp_posts pJOIN wp_term_relationships wtr ON p.ID = wtr.object_idJOIN wp_term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_idJOIN wp_terms wt ON wtt.term_id = wt.term_idWHERE wt.name = 'galerija' AND p.post_status = 'publish'ORDER BY p.post_datetnx ChrisCM for graph and help