WordPress 如何搜索文章内容而不搜索页面页面

时间:2014-12-04 11:32:46   收藏:0   阅读:104
如何在WordPress 中只搜索指定的文章类型?在http://www.wpbeginner.com上了解到通过WP提供的钩子"pre_get_posts"方法可能实现
该钩子方法可以使你在查询数据库之前对查询条件进行处理,将下面的代码放到主题的functin.php中即可实现在搜索时仅搜索文章内容

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

  17. }
  18. return $query;
  19. }
  20. add_filter(‘pre_get_posts‘,‘SearchFilter‘);

参考:






来自为知笔记(Wiz)


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!