2009-06-18 67 views
0

使用Wordpress,我需要从特定类别的帖子生成一个标签(术语)数组。例如,如果我有两个类别,“苹果”和“橙色”,我想要一个仅用于苹果类别中的帖子中的术语数组,而不是橙色类别中的术语数组。 (但是,如果中使用,应该包括这个词)WordPress的:从特定类别的帖子生成标签阵列

我试过使用少量的wordpress函数,但没有返回我想要的东西(包括tag_cloud函数)。我的猜测是我留下了一个查询调用...但到目前为止,我的所有查询都没有给我我想要的列表。

在此先感谢您的帮助。

回答

1

我不确定你可以使用WordPress提供的功能完成你想要的功能。这里,似乎你想要做什么SQL查询:

SELECT tag_terms.name, COUNT(wp_posts.ID) FROM wp_posts 

INNER JOIN wp_term_relationships AS cat_term_relationships ON wp_posts.ID= cat_term_relationships.object_ID 
INNER JOIN wp_term_taxonomy AS cat_term_taxonomy ON cat_term_relationships.term_taxonomy_id= cat_term_taxonomy.term_taxonomy_id 
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id= cat_terms.term_id 

INNER JOIN wp_term_relationships AS tag_term_relationships ON wp_posts.ID= tag_term_relationships.object_ID 
INNER JOIN wp_term_taxonomy AS tag_term_taxonomy ON tag_term_relationships.term_taxonomy_id= tag_term_taxonomy.term_taxonomy_id 
INNER JOIN wp_terms AS tag_terms ON tag_term_taxonomy.term_id= tag_terms.term_id 

WHERE cat_term_taxonomy.taxonomy='category' AND cat_terms.name='apple' AND tag_term_taxonomy.taxonomy='post_tag' 
GROUP BY tag_terms.name 

它的要点是,你与分类表中加入了wp_posts表几次获得那个职位中的ID所需的类别,然后加入这些使用分类表的ID可以多次获取相关标签。

3

这里是你如何能做到这一点使用WordPress功能

<?php 
$tags = array(); 

$posts = get_posts('category_name=new-cat&numberposts=-1'); //get all posts in category 

foreach ($posts as $post){ 
    $posttags = get_the_tags($post->ID); //check for tags 
    if ($posttags){ 
     foreach ($posttags as $posttag){ 
      $tags[$posttag->term_id] = $posttag->name; // add to array of tag ids => names 
     } 
    } 
} 
print_r($tags); 
?> 

这可能是最好的原始的SQL查询,因为这些往往会打破时,WordPress的更新其数据库架构。