搜索元查询和搜索列时WP用户查询失败

时间:2016-12-08 作者:estin92

我正在使用WP\\u User\\u Query构建高级用户搜索,但遇到了一些异常行为。我正在尝试使用一个我知道存在于user\\u nicename和user\\u email字段中的术语进行搜索,但在$args中包含meta\\u查询时未能返回。

使用下面的简单查询,在搜索用户表数据时,WordPress会按预期返回所有内容,因为该术语存在于user\\u nicename和user\\u电子邮件中。

$args = array (
    \'fields\' => [\'ID\'],
    \'count_total\' => true,
    \'order\' => \'ASC\',   
    \'orderby\' => \'display_name\',
    \'search\' => \'*\'.esc_attr( $search_term ).\'*\',
);
$wp_user_query = new WP_User_Query($args);
然而,当我在搜索中引入meta\\u查询以检查用户meta时,查询根本无法返回任何结果,即使它之前使用简单语句匹配数据。meta\\u查询使$args看起来像这样:

$args = array (
    \'fields\' => [\'ID\'],
    \'count_total\' => true,
    \'order\' => \'ASC\',   
    \'orderby\' => \'display_name\',
    \'search\' => \'*\'.esc_attr( $search_term ).\'*\',
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => \'first_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\'     => \'last_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'description\',
            \'value\' => $search_term ,
            \'compare\' => \'LIKE\'
        )
    )
);
$wp_user_query = new WP_User_Query($args);
我通过实现以下过滤器来确定要从users表中搜索哪些列:

function inc_columns( $search_columns, $search, $this ) {
    $search_columns[] = \'user_email\';
    $search_columns[] = \'user_nicename\';
    $search_columns[] = \'display_name\';

    return $search_columns;
}
add_filter(\'user_search_columns\', \'inc_columns\', 10, 3);
任何能阐明这一点的线索都是非常好的。

1 个回复
最合适的回答,由SO网友:Emil 整理而成

将元查询与关键字搜索相结合将返回与搜索查询和元查询结果都匹配的帖子(即使您使用relation => OR 在元查询中)。

This answer 介绍了一种实现帖子预期结果的方法,您需要在查询获取结果之前修改查询。但是,需要对其进行修改,以便与WP用户查询一起使用。我尝试了一种在user_nicename, user_email 和您的元字段-未经测试,因此可能需要调整。

add_action( \'pre_user_query\', \'user_meta_OR_search\');
function user_meta_OR_search($q){
    if ($search = $q->get(\'_meta_or_search\')){
        add_filter( \'get_meta_sql\', function( $sql ) use ( $search ){
            global $wpdb;

            // Only run once:
            static $nr = 0; 
            if( 0 != $nr++ ) return $sql;

            // Modify WHERE part:
            $where = sprintf(
                " AND ( %s OR %s OR %s ) ",
                $wpdb->prepare( "{$wpdb->users}.user_nicename like \'%%%s%%\'", $search),
                $wpdb->prepare( "{$wpdb->users}.user_email like \'%%%s%%\'", $search),
                mb_substr( $sql[\'where\'], 5, mb_strlen( $sql[\'where\'] ) )
            );

            $sql[\'where\'] = $where;

            return $sql;
        });
    }
}

// Then, where you do the searching:
$search_term = "test";

$args = array(
    \'fields\' => [\'ID\'],
    \'count_total\' => true,
    \'order\' => \'ASC\',   
    \'orderby\' => \'display_name\',
    \'_meta_or_search\' => \'*\'.esc_attr( $search_term ).\'*\',
    "meta_query" => array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => \'first_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\'     => \'last_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
              ),
        array(
            \'key\' => \'description\',
            \'value\' => $search_term ,
            \'compare\' => \'LIKE\'
        )
    )
);

$the_query = new WP_User_Query($args);
另一种解决方案是使用两个查询–一个是使用s 和一个搜索使用meta_query:

$args1 = array (
    \'fields\' => [\'ID\'],
    \'count_total\' => true,
    \'order\' => \'ASC\',   
    \'orderby\' => \'display_name\',
    \'search\' => \'*\'.esc_attr( $search_term ).\'*\'
);

$wp_user_query1 = new WP_User_Query($args1);
$args2 = array (
    \'fields\' => [\'ID\'],
    \'count_total\' => true,
    \'order\' => \'ASC\',   
    \'orderby\' => \'display_name\',
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => \'first_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\'     => \'last_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'description\',
            \'value\' => $search_term ,
            \'compare\' => \'LIKE\'
        )
    )
);
$wp_user_query2 = new WP_User_Query($args2);

$result = new WP_User_Query();
$result->results = array_unique( array_merge( $wp_user_query1->results, $wp_user_query2->results ), SORT_REGULAR );
$result->post_count = count( $result->results );
这将把两个查询的结果合并到包含最终搜索结果的第三个查询中。我还没有测试过这个,所以它可能需要一些调整!

相关推荐

set a user-meta key as avatar

我有一个名为“meta kay”的用户;阿凡达;;此值包含用户上载的图像的URL。如何将此元值设置为用户化身?我使用此代码,但不工作add_filter( \'get_avatar\', \'slug_get_avatar\', 10, 5 ); function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) { //If is email, try and find user ID