Check if user exists by ID (WordPress)

I know you can do username_exists but I need to check if the user exists by ID. How can I do this?

landonw said

I know you can do username_exists but I need to check if the user exists by ID. How can I do this?

Do a custom SQL query…

Edit: It’s stronger then me, here it is :slight_smile:

function username_exists_by_id($user_ID){
	
	global $wpdb;
	
	$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users WHERE ID = '$user_ID'"));

}

…so use it like…

if(username_exists_by_id(5)){ 
	//it exists
}else{
	//it doesn't
}

are you sure it works ? username_exists_by_id does not return anything … ?

Also why can’t you do it with wp built in functions ?

$aux = get_userdata( $userid );

if$aux==false){
//does not exist
}else{
//exists
}

ZoomIt said

are you sure it works ? username_exists_by_id does not return anything … ?

Also why can’t you do it with wp built in functions ?

$aux = get_userdata( $userid );

if$aux==false){
//does not exist
}else{
//exists
}

LOL, I was cleaning it up and made a doodie :slight_smile:

function username_exists_by_id($user_ID){

    global $wpdb;

    $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users WHERE ID = '$user_ID'"));

    if($count == 1){ return true; }else{ return false; }

}

P.S. Yeah i guess your way works too. I don’t do much user management stuff in WP so don’t know all the functions and ways.

i added a mobile number field in registation form and i want to know its existance like email fied, if its a new number create account else error already exists please help with the code