我正在尝试在Wordpress中扩展自定义用户搜索;即,我想按城市,州和国家搜索用户,但这些数据是序列化的。
我的例子
meta_key:pie_address_3
;
meta_value:a:6:{s:7:"address";s:18:"New York Street, 4";s:8:"address2";s:0:"";s:4:"city";s:5:"Venice";s:5:"state";s:7:"Vicenza";s:3:"zip";s:5:"36015";s:7:"country";s:5:"Italy";}
这是我正在尝试做的事情:
$my_users = new WP_User_Query(
array(
'role' => $role,
'search' => '*' . $search . '*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
'display_name'
),
'meta_query' => array(
'key' => 'pie_address_3',
'value' => $search,
'compare' => 'LIKE'
)
));
但是,当我键入某个国家或城市(我知道与某个用户相关联)时,搜索结果不返回任何内容。我怎么处理这个?谢谢!
更新
我更新了WP_User_Query
,如下所示:
$args = array(
'meta_key' => 'pie_address_3'
);
$query = new WP_User_Query($args);
$authors = $query->get_results();
foreach ($authors as $author){
$c = get_user_meta($author->id,'pie_address_3',true);
if(isset($c)){
if(empty(get_user_meta($author->id,'address',true))){
add_user_meta($author->ID,'address',$c['address']);
}
if(empty(get_user_meta($author->id,'address2',true))){
add_user_meta($author->ID,'address2',$c['address2']);
}
if(empty(get_user_meta($author->id,'city',true))){
add_user_meta($author->ID,'city',$c['city']);
}
if(empty(get_user_meta($author->id,'state',true))){
add_user_meta($author->ID,'state',$c['state']);
}
if(empty(get_user_meta($author->id,'zip',true))){
add_user_meta($author->ID,'zip',$c['zip']);
}
if(empty(get_user_meta($author->id,'country',true))){
add_user_meta($author->ID,'country',$c['country']);
};
//delete_user_meta($author->ID,'pie_address_3',$c);
}
}
$my_users = new WP_User_Query(
array(
'role' => $role,
'search' => '*' . $search . '*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
'display_name'
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'address',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'address2',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'city',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'state',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'zip',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'country',
'value' => $search,
'compare' => 'LIKE'
)
)
)
);
但是,它仍然不起作用。我在哪里做错了?
答案 0 :(得分:0)
我意识到您需要在至少一个其他列中的$search
字段 OR 内找到包含字符串pie_address_3
的用户。
不幸的是,WP_User_Query
似乎不允许列和元字段之间存在 OR 关系。实际上,结果查询的“条件”部分是这样的:
WHERE (
user_login LIKE '%$search%' OR
user_nicename LIKE '%$search%' OR
user_email LIKE '%$search%' OR
user_url LIKE '%$search%'
) AND (
wp_usermeta.meta_key = 'pie_address_3' AND
CAST(wp_usermeta.meta_value AS CHAR) LIKE '%$search%'
)
请注意两部分之间的 AND 。
在解析pre_user_query
之后,在执行查询之前触发了钩子WP_User_Query
,它通过引用传递当前的WP_User_Query
实例。因此,我们可以通过将此代码包含在我们的functions.php
文件中来拦截并将 AND 更改为 OR :
add_action('pre_user_query', 'my_custom_users_search');
function my_custom_users_search( $args ) {
$args->query_where = str_replace(') AND (', ') OR (', $args->query_where);
}
这将使以下查询起作用:
$my_users = new WP_User_Query(
array(
'role' => $role,
'search' => '*' . $search . '*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
'display_name'
),
'meta_key' => 'pie_address_3',
'meta_value' => $search,
'meta_compare' => 'LIKE'
)
);
警告:之前的操作功能可能会影响每次WP_User_Query
来电!
为了防止我们可以将一个伪参数传递给WP_User_Query
函数,并且只有在传递了该参数时才更改 OR 中的 AND 。所以这是带有and2or
伪参数的查询:
$my_users = new WP_User_Query(
array(
'and2or' => 1,
'role' => $role,
'search' => '*' . $search . '*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
'display_name'
),
'meta_key' => 'pie_address_3',
'meta_value' => $search,
'meta_compare' => 'LIKE'
)
);
这是动作功能:
add_action('pre_user_query', 'my_custom_users_search');
function my_custom_users_search( $args ) {
if( isset( $args->query_vars['and2or'] ) )
$args->query_where = str_replace(') AND (', ') OR (', $args->query_where);
}
这是搜索序列化元字段的查询(不需要像在更新时那样拆分它):
$my_users = new WP_User_Query(
array(
'role' => $role,
'meta_key' => 'pie_address_3',
'meta_value' => $search,
'meta_compare' => 'LIKE'
)
);
使用您的查询,即使在$search
参数中指定的字段,您也在搜索search_columns
。
答案 1 :(得分:0)
试试这个
$user_query = new WP_User_Query( array( 'meta_key' => 'pie_address_3', 'meta_value' => $keyword, 'meta_compare' => 'LIKE' ) );
compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', and 'NOT EXISTS'. Default value is '='.