开发者

Pull post from a specific Wordpress category

im looking to pull 3 posts from a specific Wordpress Category. At the minute I can pull 3 latest posts and display them in a flash banner using the code below.

SELECT yah_posts.*, yah_postmeta.* 
FROM yah_posts 
LEFT JOIN yah_postmeta ON yah_posts.ID = yah_postmeta.post_id 
WHERE yah_postmeta.meta_key = 'largeimage' && yah_posts.post_status = 'publish' 
ORDER BY post_date DESC LIMIT 3

I want to be able to pull 3 latest posts from a specific category instead of just 3 latest posts from every category.

I have put together this code below, but it doesn't seem to be working :(

$query = "SELECT yah_posts.*, yah_postmeta.* 
FROM yah_posts 
LEFT JOIN yah_postmeta ON yah_posts.ID = yah_postmeta.post_id 
AND LEFT JOIN $yah_term_taxonomy ON($yah_term_relationships.term_taxonomy_id = $yah_term_taxonomy.term_taxonomy_id) 
WHERE yah_postmeta.meta_key = 'largeimage' && yah_posts.post_status = 'publish' 
AND $yah_term_taxonomy.term_id = '1' 
AND $yah_term_taxonomy.tax开发者_开发百科onomy = 'category' 
ORDER BY post_date DESC LIMIT 3";


There is actually a stack exchange site just focused on wordpress questions. Looking there and taking code from this question: https://wordpress.stackexchange.com/questions/6533/modify-wordpress-sql-query-to-pull-from-within-a-category

You would need to add another JOIN:

LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)

And a couple more statements in your WHERE clause:

AND $wpdb->term_taxonomy.term_id = {term_id of the category you want}
AND $wpdb->term_taxonomy.taxonomy = 'category'


You can use wp_term_relationships in your query, take a look at wp_terms and wp_term_relationships tables in your database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜