覆盖Wordpress管理面板搜索

时间:2016-06-03 12:11:41

标签: php wordpress search

我正在尝试将post meta包含在wordpress管理面板搜索中。我做了一个研究,我发现我可以使用“pre_get_posts”过滤器将post meta设置为查询。所以我写了下面的代码

 function custom_search_query( $query ) {
    if( is_admin() && $query->is_search) {
    $custom_fields = array(
        // put all the meta fields you want to search for here
        ["key" => "property-ref-code", "compare" => "LIKE"],
        ["key" => "property-status", "compare" => "="],
        ["key" => "price", "compare" => "="],
        ["key" => "rent-price", "compare" => "="],
        ["key" => "bedrooms", "compare" => "="],
    );

    $meta_query = $query->get('meta_query');

    if (empty($meta_query))
    {
        $meta_query = array();
    }
       $meta_query[] = array('relation' => 'OR');
        foreach($custom_fields as $cf) {

            $meta_query[] = array(
                'key' => $cf["key"],
                'value' => $query->query_vars['s'],
                'compare' => $cf["compare"],
            );
        }
        $query->set("meta_query", $meta_query);
   }

}
add_filter( "pre_get_posts", "custom_search_query");

该代码现在不仅没有返回任何内容,而且还打破了wordpress搜索。我的意思是说。如果禁用此代码,则可以按帖子标题进行搜索,如果存在,则会返回一些结果。然后,如果您启用代码并按帖子标题搜索不返回任何内容。 基本上我想通过价格,参考代码等搜索属性,这些是我做过的postmeta字段。当我搜索要搜索的属性和给定的meta_keys

时,任何人都可以帮我扩展wordpress搜索

谢谢

2 个答案:

答案 0 :(得分:-1)

问题可能是if (empty($meta_query_args))总是返回true,因为$meta_query_args未定义。因此,搜索中的实际元查询将被删除。

答案 1 :(得分:-1)

- url: /
  script: www/index.php

- url: /about.php
  script: www/about.php

- url: /(.*)
  static_files: www/\1
  upload: www/(.*)
  application_readable: true