admin管理员组

文章数量:1125377

I need to get the meta value based on user id. Such as, I have meta_key called event_comments and the meta_value for this will be set to either Yes or No. I need to get that for the current user id. What I am trying is below.

$args = array(
    'user_id' => $postedby, // $postedby is the user id
    'meta_key' => 'event_comments',
    'meta_value' => 'Yes',
);
$getuser = get_users($args);

I need to get the meta value based on user id. Such as, I have meta_key called event_comments and the meta_value for this will be set to either Yes or No. I need to get that for the current user id. What I am trying is below.

$args = array(
    'user_id' => $postedby, // $postedby is the user id
    'meta_key' => 'event_comments',
    'meta_value' => 'Yes',
);
$getuser = get_users($args);
Share Improve this question asked Feb 9, 2024 at 16:57 Darth Mikey DDarth Mikey D 931 silver badge9 bronze badges 1
  • Are you looking for get_user_meta()? – Pat J Commented Feb 9, 2024 at 17:08
Add a comment  | 

1 Answer 1

Reset to default 2

get_users is for retrieving users, not meta.

To get the user meta of a user where $user_id is that users ID, you can use get_user_meta and it should work the same as the other meta functions:

$meta_value = get_user_meta( $user_id, 'the meta key', true );

You can use $user_id = get_current_user_id(); to retrieve the ID of the currently logged in user, make sure to check that the user is logged in first though.

本文标签: Get meta value based on user id