【问题标题】:wordpress search query : include tags AND limit these tags to specific categorywordpress 搜索查询:包含标签并将这些标签限制为特定类别
【发布时间】:2014-03-23 17:34:03
【问题描述】:

我想在我的 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');

谢谢你的帮助:)

【问题讨论】:

    标签: wordpress


    【解决方案1】:

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

    我会给你一些标签参数的例子,使用标签块或标签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

    【讨论】:

      猜你喜欢
      • 2022-12-17
      • 1970-01-01
      • 2016-09-10
      • 2011-07-14
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多