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: , , ,

Jay Harley is the CEO and web architect of Heaven Interactive. As a technologist, Jay specializes in creating business productivity software and offering clients high-level media consulting services. Before founding Heaven Interactive, Jay led a double life as a web consultant and MFA New Media instructor at the Academy of Art University in San Francisco. At AAU, he enthusiastically 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 squarely 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."



Leave a Reply

You must be logged in to post a comment.