16th Sep '24
/
0 comments

Related Services grouped by Service Categories on Single Branch Posts in Bricks

A user asks:

I need help please, with querying an ACF relationship field on a single CPT template.

Here's the scenario:

I have two Custom Post Types (CPTs) - "Branches" and "Services". These two CPTs are linked by an ACF relationship field, allowing me to choose which services are related to a branch and vice versa.

The "Services" CPT has a taxonomy called "Service Category", which includes categories like Massages, Facials, Nail Care, etc. This structure allows me to group the service posts.

I've created a single template for the "Branches" post type. On this template, I aim to display the "Service Category" taxonomy and list the related services linked to a branch under the taxonomy.

This Pro tutorial provides the steps for displaying related CPT 2 posts grouped by a CPT 2's taxonomy on CPT 1 post page in Bricks.

Scenario:

CPT 1 = BranchCPT 2 = ServiceCPT 2 taxonomy = Service Category

Objective: On single branches, show related services grouped by service categories.

We shall

  • Get unique service category IDs for related services of the current branch post
  • Set up a Terms query with include param set to the above
  • Set up an inner Posts query with service-category set to the current looping term and restrict the posts to related services

Single Tutorial Purchase Option

Lifetime access to this single tutorial can be purchased for $39 here.

Step 1

Register your Branch and Service CPTs and Service Category Taxonomy using ACF.

Register Branch Fields and Service Fields field groups having Related Services and Related Branches Relationship type fields respectively.

Edit each field and enable Bidirectional relationship.

Step 2

Create posts for both CPTs.

Edit posts of any one CPT and populate the Relationship field.

Step 3

Let's add a custom function to get unique service category IDs for related services of the current branch post.

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 630 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Sorting ACF Relationship Posts by Date Meta in Bricks

Sorting ACF Relationship Posts by Date Meta in Bricks

Consider the following setup: CPT: Events ACF Fields:Event Date Information (Group)|__Event Date (Date Picker)Pick Sub Events (Relationship) An Event can have 1 or more related…
How to display a Query Loop in 3 columns in Bricks

How to display a Query Loop in 3 columns in Bricks

This tutorial provides the builder settings and CSS codes that can be pasted in order to create a 3-columns query loop container inside Bricks Builder.
Categories:
Pro
Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Updated on 12 Dec 2023 Bricks Query Loop popup does not have a control for setting the number of posts to be output when the…
How to Show Future Events in Bricks

How to Show Future Events in Bricks

One of the common requirements in WordPress is being able to filter the events (or could be posts of any post type) to only those…
Categories:
Pro
Upcoming Events with ACF Date Sub field Repeaters grouped by Month Years in Bricks

Upcoming Events with ACF Date Sub field Repeaters grouped by Month Years in Bricks

In the comments section of Upcoming Events Accordion in Bricks Grouped by 'Month Year' a member asked: CPT: eventACF Field: event_dates (Repeater) with event_date sub…
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:
Color Custom Field as Section Background Color on Category/Term Archives in Bricks

Color Custom Field as Section Background Color on Category/Term Archives in Bricks

How to use a color from a taxonomy term custom field for as the hero section background color on archive pages.
Categories:
Tags:
Latest Featured Posts First in Bricks Query Loop (No Duplicates)

Latest Featured Posts First in Bricks Query Loop (No Duplicates)

In the Bricks Facebook group a user asks: We can use AI to help unfreeze our brains sometimes or at least have it write the…
Categories: