Make WordPress Core


Ignore:
Timestamp:
10/29/2014 09:40:04 PM (12 years ago)
Author:
kovshenin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/query.php

    r30017 r30094  
    222222                $this->assertEquals( $_query_vars, $query->query_vars );
    223223        }
     224
     225        /**
     226         * @ticket 23849
     227         */
     228        function test_meta_query_with_role() {
     229                $author_ids = $this->factory->user->create_many( 4, array( 'role' => 'author' ) );
     230
     231                add_user_meta( $author_ids[0], 'foo', 'bar' );
     232                add_user_meta( $author_ids[1], 'foo', 'baz' );
     233
     234                // Users with foo = bar or baz restricted to the author role.
     235                $query = new WP_User_Query( array(
     236                        'fields' => '',
     237                        'role' => 'author',
     238                        'meta_query' => array(
     239                                'relation' => 'OR',
     240                                array(
     241                                        'key' => 'foo',
     242                                        'value' => 'bar',
     243                                ),
     244                                array(
     245                                        'key' => 'foo',
     246                                        'value' => 'baz',
     247                                ),
     248                        ),
     249                ) );
     250
     251                $this->assertEquals( array( $author_ids[0], $author_ids[1] ), $query->get_results() );
     252        }
    224253}
Note: See TracChangeset for help on using the changeset viewer.