0

有没有办法按类别或标签查询帖子?我想获得三个属于同一类别或共享相同标签的最新帖子。以下是我已经拥有的代码:

$posttags = get_the_tags();
$tags = '';
if ($posttags) {
  foreach($posttags as $tag) {
    $tags .= ','.$tag->name;
  }
} 
$taglist = substr($tags, 1);
$category = get_the_category();
$posts = query_posts('&tag='.$taglist.'&orderby=date&order=DESC&posts_per_page=3&cat='.$category[0]->term_id); 

但这只会获取属于同一类别并共享相同标签的帖子。

4

2 回答 2

2

怎么样才能为类别获取数据两次,然后为标签获取数据?

$posttags = get_the_tags();
$tags = '';
if ($posttags) {
  foreach($posttags as $tag) {
    $tags .= ','.$tag->name;
  }
} 
$taglist = substr($tags, 1);
$category = get_the_category();
$tagposts = query_posts('&tag='.$taglist.'&orderby=date&order=DESC&posts_per_page=3');

$categoryposts= query_posts('&orderby=date&order=DESC&posts_per_page=3&cat='.$category[0]->term_id);

$post=array_merge($tagposts,$categoryposts);

然后你可以使用某种array_uniqueif have duplicates

于 2013-07-09T18:01:08.090 回答
0

如果您知道标签名称,则可以使用如下查询来获取标签名称与之匹配的所有帖子:

select id, post_title, guid from wp_posts p inner join wp_term_relationships r 
on p.id=r.object_id and p.post_status='publish' and p.post_type='post' 
inner join wp_term_taxonomy x on r.term_taxonomy_id=x.term_taxonomy_id and 
x.taxonomy='post_tag' inner join wp_terms t on x.term_id=t.term_id and t.name 
like '%tag_name%' group by r.object_id;

替换上面查询中的tag_name。这是参考

于 2014-01-30T11:11:33.190 回答