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...

  • Any reason why you don't can use WP_User_Query with Custom Field Parameters for this? – Nicolai Dec 19 '13 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. – D3XT3R Dec 19 '13 at 9:16
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 );
| improve this answer | |
  • It doesn't allow me to use partial matches "%$search%" as far as I know... – D3XT3R Dec 19 '13 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 '13 at 10:01
  • Ahhh @sri and @bainternet! I completely missed that addition. Thank you very much! Awesome! – D3XT3R Dec 19 '13 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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