I am looking for SQL query to select post title & post ID from a particular category. I looked at wordpress table structure but it is really confusing me.

1 Answer 1


You can use

$args = array( 'post_type' => 'post' ,'posts_per_page' => 10, 'category_name' => 'orange' );
$query = new WP_Query( $args );  

printf( '<h2>Generated SQL:</h2><pre>%s</pre>', $query->request );

to display the generated SQL query. The above example will give you:

<h2>Generated SQL:</h2>
FROM wp_posts 
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 
AND ( wp_term_relationships.term_taxonomy_id IN (123) ) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC 
LIMIT 0, 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.