[image]

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

Tags: categories, MySQL, taxonomy, wordpress

Jay Harley is a technology consultant and entrepreneur specializing in creating business productivity software. From 2002-2008, Jay worked as an MFA New Media instructor at the Academy of Art University in San Francisco, where he developed several courses in web application design and scripting, and enjoyed daily interaction with design students, offering guidance, support, and camaraderie as a media professional. Jay is now focused on providing Web 3.0 solutions to businesses in the form of collaboration and presentation software, by utilizing the power of the "Web as a Platform."



Posting your comment.

Leave a Reply

You must be logged in to post a comment.


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser
Mobilytics