wordpress搜索查询:包含标签并将这些标签限制为特定类别

时间:2014-03-23 17:34:03

标签: wordpress

我想在我的wordpress搜索查询中添加过滤功能

  

限制类别7,8中的查询帖子   并在搜索中包含类别为7,8的标签

我已经将代码限制为类别7,8但我不知道如何在查询搜索中包含标记并将这些标记限制为类别7,8

function SearchFilter($query)
 { if ($query->is_search) 
{$query->set('cat','7,8');
 $query->set('post_type', 'post');
 }
return $query;
 }
add_filter('pre_get_posts','SearchFilter');

谢谢你的帮助:)

1 个答案:

答案 0 :(得分:1)

pre_get_posts过滤器(codex page)可以访问查询变量对象($query),这是一个WP_Query对象。这意味着您可以使用其所有参数来过滤查询结果。

我将给你一些标签参数的例子,利用标签slugs或标签id(单个变量或数组)。不过,我建议您查看一下codex examples

$query->set('tag', 'bread,baking'); // Display posts that have "either" of these tags

$query->set('tag__in', array( 37, 47 )); //Display posts that are tagged with either tag id 37 or 47

$query->set('tag__and', array( 37, 47 )); //Display posts that are tagged with both tag id 37 and tag id 47: 

有关详细信息,您还可以查看WP_Query codex page