CPT Meta Searching

时间:2014-03-08 作者:jmutton

我正在开发一个使用自定义帖子类型和元数据的WP插件。我试图实现的是元数据的可搜索性。下面是我找到的一些可以很好地搜索元数据的代码,但是我无法让它在CPT页面上显示搜索字符串。

CPT Search Error

add_filter( "pre_get_posts", "custom_search_query");
function custom_search_query( $query ) {
    $custom_fields = array(
        "pctracker_company",
        "pctracker_customer",
        "pctracker_phone1",
        "pctracker_phone2",
        "pctracker_mobile",
        "pctracker_email"

    );
    $searchterm = $query->query_vars[\'s\'];
    $query->query_vars[\'s\'] = "";
    if ($searchterm != "") {
        $meta_query = array(\'relation\' => \'OR\');
        foreach($custom_fields as $cf){
            array_push($meta_query, array(
                \'key\' => $cf,
                \'value\' => $searchterm,
                \'compare\' => \'LIKE\'
            ));
        }
        $query->set("meta_query", $meta_query);
    };
}
我知道在代码中它是未设置的,但是如果不设置它,它不会显示任何结果。

有人能给我指出正确的方向吗?

非常感谢Jason

编辑:

我刚刚玩了一场,我已经成功了!

function my_search_results($query){
    if(isset($_GET[\'s\'])){
        if($_GET[\'post_type\'] == "pctracker_customers") {
            $mySearch = $_REQUEST[\'s\'];
            $query = \'select * from wp_posts,wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = "pctracker_phone1" AND wp_postmeta.meta_value LIKE "%\'.$mySearch.\'%" AND(wp_posts.post_status = "publish")\';
        }
    }
    return $query;
}
add_filter( \'posts_request\', \'my_search_results\');
编辑2:我需要搜索多个元字段,pctracker\\u phone1和pctracker\\u phone2只是一对。我编辑了SQL代码,以便它同时搜索这两个代码。然而,一旦添加了所有其他元字段,SQL代码将变得非常庞大。你知道我如何简化这一点吗:

$query = \'SELECT * FROM \'. $wpdb->prefix .\'posts,\'. $wpdb->prefix .\'postmeta WHERE \'. $wpdb->prefix .\'posts.ID = \'. $wpdb->prefix .\'postmeta.post_id AND \'. $wpdb->prefix .\'postmeta.meta_key = "pctracker_phone1" AND \'. $wpdb->prefix .\'postmeta.meta_value LIKE "%\'.$mySearch.\'%" OR \'. $wpdb->prefix .\'posts.ID = \'. $wpdb->prefix .\'postmeta.post_id AND \'. $wpdb->prefix .\'postmeta.meta_key = "pctracker_phone2" AND \'. $wpdb->prefix .\'postmeta.meta_value LIKE "%\'.$mySearch.\'%" AND(\'. $wpdb->prefix .\'posts.post_status = "publish") ORDER BY \'. $wpdb->prefix .\'posts.post_date DESC\'; 

2 个回复
SO网友:kaiser

找到的代码修改上的主查询pre_get_posts 并取消搜索条件的设置,以便WP_Query 不运行真正的搜索查询。你想要的是

// Filter the search SQL that is used in the WHERE clause of WP_Query.
apply_filters_ref_array( \'posts_search\', array( $search, &$this ) );
其中运行previously in WP_Query::get_posts(). 这之后会增加WHERE 条款:

$where .= $search . $whichauthor . $whichmimetype;
作者部分为:

" AND ($wpdb->posts.post_author = " . absint($q[\'author\']) . \')\';
部件使用的MIME类型wp_post_mime_type_where() 而且是

$whichmimetype = wp_post_mime_type_where( $q[\'post_mime_type\'], $wpdb->posts );
如果s 查询变量为set, 您可以使用orderby 并从过滤器中修改:

$search_orderby = apply_filters( \'posts_search_orderby\', $search_orderby, $this );
其结果将是first ORDER BY SQL参数后跟指定为默认值的内容ORDER BY 论点如果没有默认值,它将返回到search参数。

$orderby = $orderby ? $search_orderby . \', \' . $orderby : $search_orderby;
当然,您仍然可以使用posts_clauses 以及相关过滤器。您对结果进行了早期的实际操作检查,以检查是否通过连接

do_action( \'posts_selection\', $where . $groupby . $orderby . $limits . $join );
这将为您提供完整的最终SQL字符串。

请记住,其他插件也可以加入pre_get_posts, posts_requestposts_clauses, posts_* 过滤器,您将无法真正以通用方式解决此类冲突。

SO网友:jmutton

我需要搜索多个元字段,pctracker\\u phone1和pctracker\\u phone2只是一对。我编辑了SQL代码,以便它同时搜索这两个代码。然而,一旦添加了所有其他元字段,SQL代码将变得非常庞大。你知道我如何简化这一点吗:

function my_search_results($query){
if(isset($_GET[\'s\'])){
    if($_GET[\'post_type\'] == "pctracker_customers") {
        $mySearch = $_REQUEST[\'s\'];
        $query = \'SELECT * FROM \'. $wpdb->prefix .\'posts,\'. $wpdb->prefix .\'postmeta WHERE \'. $wpdb->prefix .\'posts.ID = \'. $wpdb->prefix .\'postmeta.post_id AND \'. $wpdb->prefix .\'postmeta.meta_key = "pctracker_phone1" AND \'. $wpdb->prefix .\'postmeta.meta_value LIKE "%\'.$mySearch.\'%" OR \'. $wpdb->prefix .\'posts.ID = \'. $wpdb->prefix .\'postmeta.post_id AND \'. $wpdb->prefix .\'postmeta.meta_key = "pctracker_phone2" AND \'. $wpdb->prefix .\'postmeta.meta_value LIKE "%\'.$mySearch.\'%" AND(\'. $wpdb->prefix .\'posts.post_status = "publish") ORDER BY \'. $wpdb->prefix .\'posts.post_date DESC\'; 
    }
}
return $query;
}
add_filter( \'posts_request\', \'my_search_results\');

结束

相关推荐

Search with filters and title

我想搜索custom_post 按标题和ACF字段。所以,我用了WP_Query WordPress函数,但我不能按标题过滤,只能按过滤器过滤。当我提交表单时,我有这样的URL:http://example.com/?s=titre&filter1=condition1&filter2=condition2&filter3=condition3 我的代码:$title = $_GET[\'s\']; $args = array( \'pagenam