WordPress Custom Query To Select Posts From A Category
Posted on February 26th, 2010 in PHP, Web Development | No Comments »
A friend asked how he could pull the three most recent WordPress posts from a particular category on a non-WordPress site. He has the ability to connect to the WordPress database. So here is the code you need to pull this off.
mysql_connect("localhost","username","password");
mysql_select_db("database");
// Change the category name to whatever category you want to use
$categoryName = 'Music';
$query = "SELECT post_title, guid
FROM
wp_posts,
wp_terms,
wp_term_taxonomy,
wp_term_relationships
WHERE
wp_terms.name = '$categoryName' and
wp_terms.term_id = wp_term_taxonomy.term_id and
wp_term_relationships.term_taxonomy_id
= wp_term_taxonomy.term_taxonomy_id and
wp_term_relationships.term_taxonomy_id
= wp_term_taxonomy.term_taxonomy_id and
wp_term_relationships.object_id = wp_posts.ID
ORDER BY post_date DESC LIMIT 3";
$q = mysql_query($query);
while ($row = mysql_fetch_assoc($q)) {
echo "
