Use a nested meta query when querying by role in WP_User_Query
.
If a user query includes a meta query together with a role argument, nest the original meta query and append the role meta query with an AND relationship. fixes #23849, #27026. git-svn-id: https://develop.svn.wordpress.org/trunk@30094 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
555d7347f9
commit
6debe759a6
@ -715,6 +715,9 @@ class WP_User_Query {
|
||||
$qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
|
||||
}
|
||||
|
||||
$meta_query = new WP_Meta_Query();
|
||||
$meta_query->parse_query_vars( $qv );
|
||||
|
||||
$role = '';
|
||||
if ( isset( $qv['role'] ) )
|
||||
$role = trim( $qv['role'] );
|
||||
@ -728,13 +731,18 @@ class WP_User_Query {
|
||||
$cap_meta_query['compare'] = 'like';
|
||||
}
|
||||
|
||||
if ( empty( $qv['meta_query'] ) || ! in_array( $cap_meta_query, $qv['meta_query'], true ) ) {
|
||||
$qv['meta_query'][] = $cap_meta_query;
|
||||
if ( empty( $meta_query->queries ) ) {
|
||||
$meta_query->queries = array( $cap_meta_query );
|
||||
} elseif ( ! in_array( $cap_meta_query, $meta_query->queries, true ) ) {
|
||||
// Append the cap query to the original queries and reparse the query.
|
||||
$meta_query->queries = array(
|
||||
'relation' => 'AND',
|
||||
array( $meta_query->queries, $cap_meta_query ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$meta_query = new WP_Meta_Query();
|
||||
$meta_query->parse_query_vars( $qv );
|
||||
$meta_query->parse_query_vars( $meta_query->queries );
|
||||
}
|
||||
|
||||
if ( !empty( $meta_query->queries ) ) {
|
||||
$clauses = $meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
|
||||
|
@ -221,4 +221,33 @@ class Tests_User_Query extends WP_UnitTestCase {
|
||||
$query->prepare_query();
|
||||
$this->assertEquals( $_query_vars, $query->query_vars );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 23849
|
||||
*/
|
||||
function test_meta_query_with_role() {
|
||||
$author_ids = $this->factory->user->create_many( 4, array( 'role' => 'author' ) );
|
||||
|
||||
add_user_meta( $author_ids[0], 'foo', 'bar' );
|
||||
add_user_meta( $author_ids[1], 'foo', 'baz' );
|
||||
|
||||
// Users with foo = bar or baz restricted to the author role.
|
||||
$query = new WP_User_Query( array(
|
||||
'fields' => '',
|
||||
'role' => 'author',
|
||||
'meta_query' => array(
|
||||
'relation' => 'OR',
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'baz',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $author_ids[0], $author_ids[1] ), $query->get_results() );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user