Sanjay Nakate
Sanjay Nakate

Reputation: 2098

How to fetch WP_User_Query with multiple role arguments

I have two user role

1) vendor 2)freevendor

Here i am trying to fetch user by there user role vendor and freevendor with the bellow code but query is fetching only freevendor.

$vendor_total_args = array (
    'role' => 'vendor',
    'role' => 'freevendor',
   'orderby'            => $orderby,
  'order'               => $order,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'pv_merchant__experiance_dropdwon',
            'value'   => $_POST[ 'pv_merchant__experiance_dropdwon1' ],
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'pv_merchant_specialization',
            'value'   => $_POST[ 'pv_merchant__experiance_dropdwon2' ],
            'compare' => 'LIKE'
        )
    )
);

So how can i fetch multiple user role vendor and freevendor using above snippet code.

Upvotes: 2

Views: 6366

Answers (2)

birgire
birgire

Reputation: 11378

Just wait few more days ... for WordPress 4.4 (scheduled early December)

Then you can just use the new attribute:

'role__in' => array( 'vendor', 'freevendor' );

of the WP_User_Query class.

Note there's also the 'role__not_in' attribute to exclude multiple user roles.

Here's the core enhancement ticket #22212, that was resolved just recently.

Upvotes: 6

Tim Sheehan
Tim Sheehan

Reputation: 4024

The solution from https://wordpress.stackexchange.com/questions/39315/get-multiple-roles-with-get-users seems to do what you want:

global $wpdb;
$blog_id = get_current_blog_id();

$user_query = new WP_User_Query( array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
            'value' => 'role_1',
            'compare' => 'like'
        ),
        array(
            'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
            'value' => 'role_2',
            'compare' => 'like'
        )
    )
) );

The trick is to get the meta query working with your existing query considering one is "and" and the other is "or". I haven't tested this but it should work:

global $wpdb;
$blog_id = get_current_blog_id();

$user_query = new WP_User_Query( array(
    'orderby'  => $orderby,
    'order'    => $order,
    'relation' => 'AND',
    array(
        'relation' => 'OR',
        array(
            'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
            'value' => 'vendor',
            'compare' => 'like'
        ),
        array(
            'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
            'value' => 'freevendor',
            'compare' => 'like'
        )
        array(
            'key' => 'genre',
            'value' => $searchvalue,
            'compare' => 'LIKE'
        )
    ),
    array(
        'key'     => 'pv_merchant__experiance_dropdwon',
        'value'   => $_POST[ 'pv_merchant__experiance_dropdwon1' ],
        'compare' => 'LIKE'
    ),
    array(
        'key'     => 'pv_merchant_specialization',
        'value'   => $_POST[ 'pv_merchant__experiance_dropdwon2' ],
        'compare' => 'LIKE'
    )
));

Upvotes: 0

Related Questions