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

Pro
Current Single Post Reference From Inside a Bricks Query Loop

Current Single Post Reference From Inside a Bricks Query Loop

Use case: Get the single post’s custom field (like ACF Repeater or Meta Box Group) array row corresponding to the loop’s counter/index
Categories:
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

This tutorial provides the PHP & JS codes that can be pasted in order to create a flying effect on map markers each time your…
How to create a dynamic infinite logo slider with ACF and Bricks

How to create a dynamic infinite logo slider with ACF and Bricks

This tutorial provides the builder settings, PHP & CSS codes that can be pasted in order to create an infinite logo slider in pure CSS…
Pro
Child Posts Section in Bricks

Child Posts Section in Bricks

This Pro tutorial provides the steps to limit the posts (can be of any post type) output by Bricks' query loop to children of the…
Categories:
Pro
Excluding Duplicate Posts from Query Loops in Bricks

Excluding Duplicate Posts from Query Loops in Bricks

How to ensure that posts are not duplicated when two query loops are used on the same page.
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…
Pro
Masonry for Query Loop in Bricks

Masonry for Query Loop in Bricks

If you are looking to implement masonry with a 'view more' button i.e., click-type of infinite scroll follow this tutorial instead. Updated on 15 Mar…
Categories: