从特定的Wordpress类别中拉出帖子

时间:2011-01-28 15:13:39

标签: php sql wordpress

我希望从特定的Wordpress类别中提取3个帖子。在那一刻,我可以使用下面的代码拉出3个最新帖子并在flash横幅中显示它们。

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

我希望能够从特定类别中提取3个最新帖子,而不是每个类别中只有3个最新帖子。

我已将下面的代码放在一起,但它似乎不起作用:(

$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.taxonomy = 'category' 
ORDER BY post_date DESC LIMIT 3";

2 个答案:

答案 0 :(得分:1)

实际上有一个堆栈交换站点专注于wordpress问题。看那里并从这个问题中获取代码: https://wordpress.stackexchange.com/questions/6533/modify-wordpress-sql-query-to-pull-from-within-a-category

您需要添加另一个JOIN:

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

WHERE子句中还有一些语句:

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

答案 1 :(得分:1)

您可以在查询中使用wp_term_relationships,查看数据库中的wp_terms和wp_term_relationships表。