Skip to content

Instantly share code, notes, and snippets.

@davestewart
Created August 8, 2014 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davestewart/bf85440a4a8561ab896b to your computer and use it in GitHub Desktop.
Save davestewart/bf85440a4a8561ab896b to your computer and use it in GitHub Desktop.
SQL to select post data by category, tag or post format
SET @slug = "tools";
SET @taxonomy = "category"; # post_tag, category, post_format
SELECT
post_name,
post_title,
post_content,
post_content_filtered,
post_excerpt,
post_status,
post_date,
post_date_gmt,
post_modified,
post_modified_gmt,
post_type,
to_ping,
pinged
FROM wp_posts
LEFT JOIN wp_term_relationships ON
(wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON
(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms ON
(wp_term_taxonomy.term_taxonomy_id = wp_terms.term_id)
WHERE wp_posts.post_status = 'publish'
AND wp_term_taxonomy.taxonomy = @taxonomy
AND wp_terms.slug = @slug
ORDER BY post_date DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment