Help - Search - Members - Calendar
Full Version: MySQL query help
Invision Power Services > Community Forums > Community Web Design and Coding
=Charles
I'm using Wordpress as a means to power a site. The only thing is that I am using my own front end, the Wordpress front end is not useful for this project.

In Wordpress I have created some categories. -> News, Gigs, etc.

I want to pull posts depending on their categories. For instance, I want to pull all category 2 posts as they are "News". The only problem is that my knowledge of MySQL is limited and joins are most probably going to be needed.

I can supply the database structure if needed.

Here is the table that ties the posts to the categories, wp_post2cat:
CODE
rel_id post_id category_id
1 1 1
2 3 2
4 4 3


Obviously the post_id relates to the post ID. I don't understand how to query for the latest 5 news posts that have a status of "publish".

Thanks to anyone who reads, helps, etc.
Starnox
CODE
SELECT posts.field FROM wp_post2cat, posts WHERE posts.id = wp_post2cat.id AND wp_post2cat.category_id = 2 LIMIT 0,5


??

replace posts with the posts table name, and add and field you want to select like title, the post etc.
IAIHMB
Just ran this through my WordPress installation and it seems to do the trick:

CODE
SELECT wp_posts.* FROM wp_posts, wp_post2cat WHERE wp_posts.ID = wp_post2cat.post_id AND wp_post2cat.category_id = 2 LIMIT 0,5


Edit: Damnit Starnox! tongue.gif
=Charles
Thanks guys. blushing.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.