Skip to content

Instantly share code, notes, and snippets.

@robwent
Created January 10, 2018 18:16
Show Gist options
  • Save robwent/33bc025005d2a9144e1c055e54b354df to your computer and use it in GitHub Desktop.
Save robwent/33bc025005d2a9144e1c055e54b354df to your computer and use it in GitHub Desktop.
Select Wordpress posts by category ID
SELECT * 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)
WHERE wp_posts.post_type = 'post'
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id = 48
ORDER BY post_date DESC
@vinaysyadav
Copy link

Useful script

@obzc
Copy link

obzc commented Dec 6, 2019

I need sql code that updates post_type by category id.

@robwent
Copy link
Author

robwent commented Dec 6, 2019

Maybe:

UPDATE wp_posts SET `post_type`='new_post_type'
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)
WHERE wp_posts.post_type = 'post'
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id = 48

@AmirHosseinAminiDev
Copy link

thank you so much! your code helped me today!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment