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?

Just add this in child theme‘s functions.php or a code snippets plugin:

// Function to check if WooCommerce cart is empty.
function bl_is_cart_empty() {
	if ( function_exists( 'WC' ) ) {
		return WC()->cart->is_empty();
	}

	return true;
}
// 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)}

Get full access to all 220+ Bricks tutorials with BricksLabs Pro

Leave the first comment