In BricksLabs' Facebook group a user asks:
I have a Product (Metabox CPT) that is connected to a Creator (Metabox CPT) via Relationships.
In the Product single page, I want to list 'Other products from the same Creator' using a loop item.
How do I query this?
From what I see, this is not possible to do using the built-in controls of the Query interface in Bricks.
We need to:
- get connected creators from the current product
- get products connected to the above creators
- remove the current product from the above products
- modify the query to show only the above products
With post type A to post type B Meta Box relationship, this Pro tutorial provides the steps to use Meta Box' API for querying items of post type B connected to the current item of post type A (forward query), then doing a backward query to get all items of post type A connected to the matching post type B items and removing any occurrences of current post type A items to finally pass them to Bricks'
In the Meta Box documentation, the example provided is Posts connected to Pages.
Per the original question, the relationship is: Products connected to Creators.
In the example below, we are going to do Events connected to Testimonials (I happen to have entries for these already present in my local test site and just went with this setup).
Here is the notes I put together of the problem statement:
We will also get the count of the matching events and ensure that the Section in which we have the Posts element (which could also be a Query Loop instead) only outputs if there is at least 1 event to be shown.
Install and activate Meta Box and Meta Box AIO.
Create your two post types.
Go to Meta Box → Relationships and create one.
Create a few testimonial entries:
and some event entries:
Add a new Bricks template named "Single Event" of the type "Single" and edit it with Bricks.
Click the Settings (gear) icon → TEMPLATE SETTINGS → CONDITIONS and add a Post type = Events.
Add a Section and inside its Container, Post Title and Post Content elements.
Add another Section and inside its Container, a "Related Events" Heading.
Add either a Posts element or set up a Query Loop.
Click the Query loop icon and select Post type: Events.
Since we are going to modify this query later by passing in an array of event IDs as the value of
post__in parameter, enabling "Exclude current post" control here is not going to have any effect.
Add the following in your child theme's
functions.php or a code snippets plugin:
This is a BricksLabs Pro tutorial.
For full access login below or create your BricksLabs Pro account