Select Posts within a Wordpress Category in MySQL
My SQL knowledge is fairly basic. Â I am an interactive designer, not a strict low-level coder, so I am looking for solutions that do not require cryptic or highly detailed SQL, when I want to access a DB system.
If you want to access the WP MySQL database tables, you can of course include the ‘wp-db.php’ file and use their class which is a wrapper (as detailed on the Wordpress.com site) for accessing the DB. Â I tried this, and within the system I am working on (PHP files pulling posts from a certain category on a WP install into a completely different site), this didn’t work for me.
Again my SQL is quite basic, but you should be able to see the table relationships based on my ‘kiddie code’. Â ![]()
Below is the code to pull in articles from a category:
SELECT DISTINCT post_name FROM `wp_posts`, wp_term_relationships, wp_term_taxonomy WHERE wp_term_relationships.object_id = wp_posts.id AND post_status = ‘publish’ and post_type = ‘post’ AND wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id AND wp_term_taxonomy.taxonomy = ‘category’ AND wp_term_taxonomy.term_id = YOUR_CATEGORY_ID
![[image]](http://mowser.com/img?url=http%3A%2F%2Fwww.heaveninteractive.com%2Fimages%2Famazon%2F51Vo7OODj6L._SL160_.jpg)

Comments(0)