WordPress.org

Make WordPress Core

Changeset 30094


Ignore:
Timestamp:
10/29/2014 09:40:04 PM (6 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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r30033 r30094  
    716716        }
    717717
     718        $meta_query = new WP_Meta_Query();
     719        $meta_query->parse_query_vars( $qv );
     720
    718721        $role = '';
    719722        if ( isset( $qv['role'] ) )
     
    729732            }
    730733
    731             if ( empty( $qv['meta_query'] ) || ! in_array( $cap_meta_query, $qv['meta_query'], true ) ) {
    732                 $qv['meta_query'][] = $cap_meta_query;
     734            if ( empty( $meta_query->queries ) ) {
     735                $meta_query->queries = array( $cap_meta_query );
     736            } elseif ( ! in_array( $cap_meta_query, $meta_query->queries, true ) ) {
     737                // Append the cap query to the original queries and reparse the query.
     738                $meta_query->queries = array(
     739                    'relation' => 'AND',
     740                    array( $meta_query->queries, $cap_meta_query ),
     741                );
    733742            }
    734         }
    735 
    736         $meta_query = new WP_Meta_Query();
    737         $meta_query->parse_query_vars( $qv );
     743
     744            $meta_query->parse_query_vars( $meta_query->queries );
     745        }
    738746
    739747        if ( !empty( $meta_query->queries ) ) {
  • 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.