Get User Meta for the Current Post Author in WordPress

Updated on 29 Jul 2024

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 );
}

Whitelist the bl_get_user_meta function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_get_user_meta'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_get_user_meta. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

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 access to all 524 Bricks code tutorials with BricksLabs Pro

Leave the first comment