Question

I'm working on a Buddypress / Wordpress site, and using

get_queried_object()->ID; 

to grab the user's ID, but I get different results on different pages. I've gotten 47, 59, and 2. It should be 1 since I'm the admin. Any idea why it changes?

Était-ce utile?

La solution

Probably you are looking for get_current_user_id() function, not get_queried_object() function. get_queried_object() retrieve the currently-queried object. Instead you should use get_current_user_id( )

$user_id = get_current_user_id();
if ($user_id == 0) {
    echo 'You are currently not logged in.';
} else {
    echo 'You are logged in as user '.$user_id;
}

In order to info about the logged in user try

global $current_user;
get_currentuserinfo();

echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top