Skip to content

Instantly share code, notes, and snippets.

@minio
Created October 14, 2013 21:30
Show Gist options
  • Save minio/6982551 to your computer and use it in GitHub Desktop.
Save minio/6982551 to your computer and use it in GitHub Desktop.
query to get all posts and associated categories from Wordpress database
$all_posts = $wpdb->get_results("
SELECT wp_posts.ID,wp_posts.post_date,wp_posts.post_title,wp_posts.post_name,
wp_terms.name,wp_terms.slug,wp_term_taxonomy.description
from wp_posts
join wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id
join wp_terms on wp_term_relationships.term_taxonomy_id = wp_terms.term_id
join wp_term_taxonomy on wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE wp_posts.post_status = 'publish'
ORDER BY wp_terms.slug,wp_posts.ID;
", ARRAY_A);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment