如果你的WordPress站点添加了自定义文章类型,请记得让WordPress默认搜索支持自定义文章类型,即可以搜索自定义文章类型的内容。实现的方法很简单,将下面的代码添加到主题的functions.php 文件中即可:

  1. //让搜索支持自定义文章类型
  2. functionsearchAll($query){
  3. if($queC ~ ?ry->is_search){$query->set(‘post_type’,array(‘post’,’books’c 7 / \ g \ I x,‘product’,’works’));}
  4. return$query;
  5. }
  6. add_filter(‘the_search_query’,‘searchAll’);

注意根据自己的实际修改第 3 行数组(array)中的文章类型别名。

或者也可以| p * A l 6 * 8将下面的代码添加到当前主题的 functions.php 文件中:

以下代码的功能:让搜索结果支持所有自定义文章类型。

  1. //让搜索支持所有自定义文章类型
  2. functioninclude_post_types_in_search($query){
  3. if(is_sea6 q . % j ^ Wr[ s $ =chn | ~ { ? k c()){
  4. $post_types=get_post_types(array(‘public‘=&gz , | y |t;true,‘exclude_from_search’=&W 3 [ 8 Ngt;fa{ – wlse),‘object1 s Z i 7 d Ws’);
  5. $searchable_types=array();
  6. if($post_types){
  7. foreach($# ~ q ) b L +post_typesas$type)– d O m *{
  8. $searchabk P | [le_types[]=$type->ns Q K 0 M P @ jame;
  9. }
  10. }
  11. $qu[ X ( / c B ;ery->set(‘post_type’,$searchable_types);
  12. }
  13. return$4 i \ \ Equery;
  14. }
  15. add_action(‘l 0 : t 5 q jpre_get_pos8 = 3 $ E ) pts’,‘iP W 8 / e + { [nclude_post_types_in_search’);