Post Data for the Current User in Bricks with Meta Box

In the Bricks Facebook group a user asked:

I need help with Metabox and Bricks!

I have created a CPT “SalesRep” with multiple custom fields that has relationship with User.

In User Meta I have a Custom Field SalesRep, a dropdown that is being fed by “SalesRep” CPT Title (it’s a Post type Custom field)

In Bricks page I want to display Sales Rep that is assigned to the current logged in user along with their (SalesRep’) contact details. I have no problem displaying the SalesRep name as it is anyway stored in the UserMeta but I have no idea how to pull in their phone number and email which are Custom field for SalesRep CPTs.

Any ideas?

Consider the following scenario.

  • CPT: SalesRep
    • Custom fields: Phone number and Email
  • User Meta: SalesRep (Type: Post)
  • Sarah Johnson (a post of the SalesRep CPT) has been set as the Sales Rep for User A
  • David Wilson has been set as the Sales Rep for User B

Requirement: When User A logs into the site and visits the Page (say, “Profile”) he/she should see Sarah Johnson as their Sales Rep. When User B logs into the site and visits the Profile page, he/she should see David Wilson as their Sales Rep.

This Pro tutorial shows how we can configure a Bricks query loop to output the post data of the SalesRep CPT that is associated with the current logged-in user.

Setup

SalesRep CPT:

SalesRep CPT’s custom fields:

‘User Fields’ field group:

When editing User A:

When editing User B:

After implementing the tutorial, Profile page on the front end:

When User A is logged in:

When User B is logged in:

Bricks editor

Add a Section and inside its Container, a h2 Heading that reads say, “Your Sales Rep”.

Add a Block below it and enable query loop.

Click the loop icon. Enable PHP query editor and paste:

$post_id = rwmb_meta( 'salesrep', [ 'object_type' => 'user' ], get_current_user_id() );

if ( $post_id ) {
	return [
		'post_type' => 'salesrep',
		'posts_per_page' => 1,
		'no_found_rows' => true,
		'p' => $post_id,
	];
} else {
	return [
		'post__in' => [ 0 ],
	];
}

Add a h3 Heading inside the Block and change its text to:

{post_title:link}

If you want to show the featured image, add an Image element and set its source accordingly.

Add any other elements as needed and map them to the custom field values.