2

I am having an issue:

I don't know why this happens.

Regarding user metadata, when should I use one or the other?

I tried this:

get_the_author_meta('afzfp_user_status', $user->ID);
get_user_meta('afzfp_user_status', $user->ID, true);

It returns:

String "the meta value"
bool (false)

UPDATE:

(As pointed by @shanebp) - The parameters order is different, that is why my code is not working as expected:

get_the_author_meta('afzfp_user_status', $user->ID);
get_user_meta($user->ID, 'afzfp_user_status', true);

The question itself still remains: What is the difference between them and when is it recommended to use one or the other?

2
  • Can you include the code you ran for both of them? The parameters and how they're called are important
    – Tom J Nowell
    Jun 9, 2020 at 20:54
  • @TomJNowell Thanks for your attention, I updated my question with sample code. Jun 9, 2020 at 21:04

2 Answers 2

1

get_the_author_meta: retrieves the requested data of the author of the current post.

  • Parameters: ( string $field = '', int|false $user_id = false )

get_user_meta: retrieves user meta field for a user.

  • Parameters: ( int $user_id, string $key = '', bool $single = false )

Example:

John is an logged in user reading a post written by Mary.

  • get_the_author_meta will return the value of a field (string) related to Mary, the author.
  • get_user_meta will return the value of one (string) or more (array) meta data fields related to John.

Snippets:

get_the_author_meta

Get the email address for the author of the current post* and store it in the $author_email variable for further use:

$author_email = get_the_author_meta( 'author_email' );

*If used within The Loop, the user ID need not be specified, it defaults to current post author. A user ID must be specified if used outside The Loop.

get_user_meta

Get the last name for user id 2 and store it in the $user_last variable:

$user_last = get_user_meta( 2, 'last_name', true );

Reference:

get_the_author_meta: https://developer.wordpress.org/reference/functions/get_the_author_meta/

get_user_meta: https://developer.wordpress.org/reference/functions/get_user_meta/

2

Your parameter order is wrong for get_user_meta. The user id should be first.

Try: var_dump(get_user_meta( $user->ID, 'afzfp_user_status', true));

2
  • This lack of focus should have an associated punishment. Thanks for your help! By the way I will wait before marking your answer as accepted because I still don't know when I should use one or the other. There must be a purpose in having defined two functions to retrieve user metadata. The main difference I see is one accepting the third "return single" parameter, but not sure if that's the whole reason. Jun 10, 2020 at 7:51
  • The difference is that get_user_meta has an optional third parameter and the $user_id is required rather than being optional.
    – shanebp
    Jun 10, 2020 at 15:36

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.