2nd Feb '24
/
1 comment

Related Posts Grouped by Custom Field Value when using ACF Relationships in Bricks

In the Bricks Facebook group a user asks:

Let's summarize the situation.

  • Location CPT (view in question)
    • Related Boat field (Relationship)
  • Boat CPT
    • Related Location field (Relationship)
    • Cabin field (Select)
  • Relationship

with the CPTs and field groups created using ACF.

Requirement: On single locations, show related boats grouped by cabin field.

Rough outline of what we should do:

  • Get array of boat IDs related to the current location
  • Loop through the above boat IDs, get the Cabin field value for each and construct an associative array of cabins => boat IDs.
  • Outer query loop: Custom cabins query type
    • Inner query loop: boats where post__in => bl_get_boats_by_cabin( 'outer cabin' )

Sample data (numbers in brackets are post IDs):

  • Location 1 (199): Boat 1 (202), Boat 3 (204), Boat 4 (205)
  • Location 2: Boat 2 (203), Boat 3 (204)
  • Location 3: Boat 4 (205)
  • Boat 1 (202): V-berth (cabin)
  • Boat 2 (203): Saloon (cabin)
  • Boat 3 (204): Saloon (cabin)
  • Boat 4 (205): V-berth (cabin)

This Pro tutorial shows how related posts of a different CPT can be grouped by post meta when viewing single posts of a CPT in Bricks.

Step 1

Create location and boat CPTs.

Location Fields:

Boat Fields:

Step 2

Edit your Location CPT and Boat CPT entries and populate the custom fields for each.

Step 3

Let's create a custom function that loops through the boats related to the current location and returns an associative array having the value of cabin field as the array keys and an array of related boat post objects as the array values.

Ex.: For 'Location 1' post:

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

(code snippet title suggestion: Get cabins => boat IDs for current location)

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..

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:
Pro
Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

How to output any post type, with the taxonomy terms as Tab titles and posts inside tables with sortable columns.
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:
Pro
How to Insert an Ad or an Element Between Posts in Bricks

How to Insert an Ad or an Element Between Posts in Bricks

How to insert an image or any Bricks element between posts output by a query loop.
Categories:
Tags:
Pro
Upcoming Events Grouped by ‘Month Year’ in Bricks

Upcoming Events Grouped by ‘Month Year’ in Bricks

A custom query loop type for showing future events based on a date custom field using three nested query loops.