Table - wp_usermeta

user_id  meta_key      meta_value
10       login_status  0  
10       active_code   3f7431e226893f16cbe44424850d00ad

I need to select user_id where login_status = 0 and active_code = 3f7431e226893f16cbe44424850d00ad

Ex: SELECT user_id FROM usermeta WHERE active_code ='3f7431e226893f16cbe44424850d00ad' and login_status='0'

Tried below query but no success.

$querystr = "
        SELECT $wpdb->usermeta.user_id, $wpdb->usermeta.meta_key
        FROM $wpdb->usermeta
        WHERE $wpdb->usermeta.meta_value = '3f7431e226893f16cbe44424850d00ad`
        AND $wpdb->usermeta.meta_value = '0' ";

How can I simply fetch the login_status value along with active_code.


  • the SQL statement will be a bit more complex than this; is there a particular reason you're avoiding WP_User_Query? Feb 6, 2015 at 13:00
  • I just do not want to save my activation_cdoe and login_status in wp_user table, but in usermeta. how its possible using WP_User_Query ? Feb 6, 2015 at 13:03
  • 1
    the code you've provided is for retrieving data from the database, not storing it. please re-write your question clarifying exactly what you're trying to do. Feb 6, 2015 at 13:05
  • data is already stored at the time of registration. during login need to fetch that data and change status = active where code matches Feb 6, 2015 at 13:06
  • that should be happening automatically afaik Feb 6, 2015 at 17:38

1 Answer 1


All of this can be done using WP_User_Query:

$users = new WP_User_Query(array(
        array( 'key' => 'login_status', 'value' => '0' ),
        array( 'key' => 'active_code', 'value' => '3f7431e226893f16cbe44424850d00ad' )

This will return a list of users that meet your requirements. Then you can get the ids like so:

$user_ids = wp_list_pluck( $users->get_results(), 'ID' );

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.