0

Here's my Query... I'm looking to narrow this query down to users of one specific role ("meta_key = 'wp_capabilities' AND meta_value = 'employee'")...how do I add that additional conditional to this string?

$get_users = "SELECT * FROM $wpdb->users
WHERE ID = ANY (
SELECT user_id FROM $wpdb->usermeta
WHERE meta_key = '$meta_key'
AND meta_value LIKE '%$search%'
) ORDER BY user_nicename ASC LIMIT 10000";

Thanks in advance...

2
  • Any reason why you don't can use WP_User_Query with Custom Field Parameters for this? Dec 19, 2013 at 8:54
  • I want to be able to search a partial match. For example: If I'm searching a username and I know what it begins with but not what it ends with I can search for part of it and get results. Dec 19, 2013 at 9:16

1 Answer 1

0

WordPress Provides a native functions to query these kind of things very easily like WP_User_Query

$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => $meta_key1,
            'value'   => $search1,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => $meta_key2,
            'value'   => $search2,
            'compare' => '='
        )
    )
 );
$user_query = new WP_User_Query( $args );
3
  • It doesn't allow me to use partial matches "%$search%" as far as I know... Dec 19, 2013 at 9:14
  • 1
    @D3XT3R It should allow you to do partial matching. That is what Bainternet is trying to show by the using like. For $search1 parameter you could have something like \'\%ABC\%\' where the backslash is the escape character in php.
    – sri
    Dec 19, 2013 at 10:01
  • Ahhh @sri and @bainternet! I completely missed that addition. Thank you very much! Awesome! Dec 19, 2013 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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