WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As far as I know, the_author_meta() automatically displays the data while get_the_author_meta() has to be echo-d, it can also be saved as variable and do something with it.

Is there something else to it that is not documented in codex?

Also, is one or the other function heavier or is there a third better alternative?

This is not the case of premature optimization - I need to dispay a lot of author data.


References:

the_author_meta()

get_the_author_meta()

share|improve this question
up vote 4 down vote accepted

If you look at the source code for the_author_meta(), it simply, in essence, just echo's the result from get_the_author_meta().

WordPress has quite a lot of functions where functions with a the_* prefix simply echos the results from its get_* counter parts. The get_* prefix is almost always used for functions which return its results.

There is really no performance differences between the two functions here, and you can use anyone to your liking, although each actually has each own specific use. Use the_author_meta() if you need to display the data, it looks a bit nutty to use echo get_the_author_meta(). Use get_the_author_meta() if you need to store the data for later use, or in shortcodes which require returned content

EDIT

You can always look up the source code on https://developer.wordpress.org, something you should always do. Never ever take something for granted. Just an example of the above, get_the_content() return unfiltered, raw post content, the_content returns the filtered content from the get_the_content(), so they are vastly apart in what they do

EDIT 2

....is there a third better alternative

If you look at the source code from get_the_author_meta(), it uses get_userdata() which returns all the fields you might be interested in

share|improve this answer
    
See my updates. – Pieter Goosen Feb 15 at 17:37
    
Hmm, get_userdata() seems like the best solution in cases where you need to display most of the user data (e.g profile page). Then it doesn't need to use all the other functions that seems like a waste of resources (minor waste but still) and are just used to make programmer's life easier. – N00b Feb 15 at 17:45
1  
While get_the_author_meta() always uses get_userdata() and I always need to deal with more than one type of user data, I think I'll avoid get_the_author_meta() completely. That answers the question for me. – N00b Feb 15 at 17:48

With this sort of question, the simplest thing to do is to look it up in Codex then follow the link to see the function in source.

There we can see that the_author_meta just calls get_the_author_meta and passes the result through a filter.

function the_author_meta( $field = '', $user_id = false ) {
    $author_meta = get_the_author_meta( $field, $user_id );

    echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
}

Another important thing to note is that, like post metadata, when you request a single meta item, all of it is loaded and cached, so it doesn't really matter if you fetch one meta field or 50, it's just a single trip to the database to fetch it all in both cases.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.