9th Oct '24
/
2 comments

Post Data for the Current User in Bricks with Meta Box

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:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering a query loop to output only those posts with the value of a Select subfield of a Group field set to Yes when using…
Categories:
Tags:
Pro
MB Favorite Posts in Bricks

MB Favorite Posts in Bricks

This tutorial explores using MB Favorite Posts in Bricks, including outputting the list of favorites via a query loop.
Categories:
Pro
Sort Posts by Meta Box Group Sub Field in Bricks

Sort Posts by Meta Box Group Sub Field in Bricks

This Pro tutorial provides the steps to order posts in Bricks by the value of a Number-type of sub field that is inside a Meta…
Pro
6 Random Posts from Tag A + 6 Random Posts from Tag B in a Single Bricks Query Loop

6 Random Posts from Tag A + 6 Random Posts from Tag B in a Single Bricks Query Loop

Displaying two set of random posts, inside one query loop.
Categories:
Pro
HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

How to set the source of Bricks’ Image Gallery element on a post to images in the selected HappyFiles Folder(s) via a Meta Box Taxonomy…