Get User Meta for the Current Post Author in WordPress

Looking to get user meta for the current post author either on single posts or in the archive when in the loop?

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

// Get the specified user meta for the current post author
function bl_get_user_meta( $meta ) {
	$author_id = get_the_author_meta( 'ID' );

	return get_user_meta( $author_id, $meta, true );
}

Sample usage:

echo bl_get_user_meta( 'user_country' );

where user_country is the name/ID of the user custom field.

If you are a Bricks user and would like to conditionally output an element if a specific user field for the post’s author contains the specified string, apply a dynamic condition in Bricks template like this:

{echo:bl_get_user_meta(user_country)}

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

Leave the first comment