Limiting the Query Loop Posts Output to Post IDs From a Custom Field in Bricks

A user asks:

I have a list of posts (IDs) in a custom field of a CPT. On a Bricks template, I need to run a query for the IDs in the custom field only. How can I tell the query loop builder to use the IDs from the custom field only?

In PHP, I’d add this (custom field is an array):

post__in => $custom_field

But How can I add this to Bricks? Do I have to create a custom Query in PHP and hook that into the builder, or is there an easier way?

Recently Bricks introduced the PHP query editor that lets us modify the query parameters right within the query dialog of the builder rather than having to do it outside using a code snippets plugin or child theme’s functions.php.

Generally speaking, a “Post” type of Meta Box field or a Relationship is better suited when the requirement is to select one or more posts (of any post type) when editing a post (of any post type). But in this case, let’s go with a “Select advanced” field type per the user’s question.

The above field group’s location is set to Post type = Page (page).

Post IDs field being populated on a sample Page:

Set up a query loop in the Bricks template that applies to all single pages of the page post type.

Click on the query icon and toggle “Query editor (PHP)” on.

Paste:

return [
  'post__in' => rwmb_meta( 'post_ids' ),
  'posts_per_page' => '-1',
];

where post_ids is the ID of the field group.

That will tell Bricks (to tell WordPress) to fetch all the posts whose IDs are in the array of posts returned by the value of that custom field.

Get access to all 526 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Ordering Meta Box group rows by a sub field in Bricks

Ordering Meta Box group rows by a sub field in Bricks

In the BricksLabs Facebook group a user asks: Any idea how to set the sort order of a cloneable group in the query? In this…
Pro
Update Post Meta From Another Custom Field’s Value on Post Publish/Update when using Meta Box

Update Post Meta From Another Custom Field’s Value on Post Publish/Update when using Meta Box

In the Meta Box Facebook group a user asks: Hi all, wondering if I can get some help. For custom field 1, I am using…
Categories:
Tags:
Pro
Meta Box Checkbox List Query Type in Bricks

Meta Box Checkbox List Query Type in Bricks

This Pro tutorial provides the steps to add a new query type in Bricks builder for displaying the values of a Checkbox List type of…
Categories:
Pro
Querying Posts by Date/Datetime Custom Fields in Bricks

Querying Posts by Date/Datetime Custom Fields in Bricks

This Pro tutorial shows how to configure ACF and Meta Box to query posts in Bricks by custom field(s) of the type Date/Date Time Picker…
Categories:
Tags:
Pro
Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Update on 23 Aug 2023: Added steps for ACF. Bricks builder v1.8.4 introduced an pretty useful filter called bricks/active_templates that flew under the radar. This…