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 612 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
How to add a layout toggle on query loops in Bricks

How to add a layout toggle on query loops in Bricks

In this tutorial, we’ll show you how to change the layout of your query loop container based on the click of an icon toggle sitting…
Categories:
Adding any Custom WP_Query loop to Bricks’ Query Loop

Adding any Custom WP_Query loop to Bricks’ Query Loop

Sometimes we may need to create a custom post loop using Bricks' query loop feature, but wish to use our own WP_Query code for more…
Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…
Pro
Parent post Bricks query loop

Parent post Bricks query loop

Updated on 5 Feb 2024 In the BricksLabs Facebook group a user asks: Is there a way to output the data of a parent post…
Categories:
Pro
Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Creating a custom condition that returns true or false depending on whether today falls within 5 days before any holiday dates selected on a Meta…
Categories:
Tags: