0

This question already has an answer here:

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.

marked as duplicate by kaiser Nov 23 '15 at 15:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Duplicate of: wordpress.stackexchange.com/a/88158/22728 – Mayeenul Islam Nov 23 '15 at 12:54
  • The problem is that the SQL AND clause will question if both arguments apply. What you need to use is OR to match both roles. Also, you should sanitize your $_POST data with PHPs filter_input() for e.g.. This could be unsafe data from a form request and a perfect target for a SQL injection. – kaiser Nov 23 '15 at 15:22
0

WP_User_Query only accepts a single 'role' argument, and since your array has the same key twice only the second is getting used:

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

Is the same as:

array(
    'role' => 'freevendor',
);

You could make two calls, one for each role set:

$user_query_args = array (
    'role'       => 'vendor',
    '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'
        )
    )
);

$vendors = new WP_User_Query( $user_query_args )->results;

$user_query_args['role'] = 'freevendor';

$freevendors = new WP_User_Query( $user_query_args )->results;

And then combine the two result sets. Or take a look at the comment that shows how to write a meta query combining the two roles.

Not the answer you're looking for? Browse other questions tagged or ask your own question.