如何在WordPress 中只搜索指定的文章类型?在http://www.wpbeginner.com上了解到通过WP提供的钩子"pre_get_posts"方法可能实现

该钩子方法可以使你在查询数据库之前对查询条件进行处理,将下面的代码放到主题的functin.php中即可实现在搜索时仅搜索文章内容

/**
*[只对指定的类型进行搜索]
*@param[type] $query [搜索的参数]
*/
function SearchFilter($query){
//仅搜索时
if($query->is_search){
//设定指定的文章类型,这里仅搜索文章
$query->set('post_type','post');
//指定文章和自定义类型
//$query->set('post_type', array('post','custom-post-type'));
//排除指定的文章ID号
//$query-->set('post__not_in', array(10,11,20,105));
//搜索指定的类型
//$query->set('cat','8,15');
//搜索条件....

}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

 

 
参考:
 
 
 
 





相关文章:

  • 2021-11-23
  • 2021-05-10
  • 2021-11-20
  • 2021-09-27
  • 2021-12-16
  • 2021-12-26
  • 2021-09-04
  • 2022-12-23
猜你喜欢
  • 2021-07-13
  • 2021-09-08
  • 2021-05-14
  • 2021-07-17
  • 2021-10-23
  • 2021-11-05
相关资源
相似解决方案