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 = Branch
CPT 2 = Service
CPT 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 524 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Search Results Grouped by Post Types in Bricks

Search Results Grouped by Post Types in Bricks

Updated on 08 Oct 2024 This Pro tutorial provides the steps to arrange the search results by specified post types in a Bricks site. We…
Pro
Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

On product pages, we show all the FAQs associated with the current WooCommerce product's product categories.
Categories:
Pro
Conditionally Hiding Bricks Filters based on Select Filter Value

Conditionally Hiding Bricks Filters based on Select Filter Value

How show or hide Bricks' filter based on the selection made by another filter.
Categories:
Tags:
Using WP Grid Builder Facets with Bricks’ Query Loop

Using WP Grid Builder Facets with Bricks’ Query Loop

Update: This tutorial was written before WPGB had a Bricks add-on. Now, it is advisable to use that add-on instead. Get it from here. Updated…
Categories:
Bricks Templates Query Loop

Bricks Templates Query Loop

How display Bricks Templates with saved screenshots in a query loop.
Categories:
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…