11th Dec '23
/
0 comments

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 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
Pro
HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

How to set the source of Bricks’ Image Gallery element on a post to images in the selected HappyFiles Folder(s) via a Meta Box Taxonomy…
Pro
Filtering Query Loop Posts by a Non-empty Custom Field in Bricks

Filtering Query Loop Posts by a Non-empty Custom Field in Bricks

This Pro tutorial shows how we can prefilter the results of a query output by a Bricks query loop to only those for which the…
Categories:
Tags:
Pro
Conditional Rendering Based on Current Date and Time in Bricks when Using Meta Box

Conditional Rendering Based on Current Date and Time in Bricks when Using Meta Box

A Pro member of our site asks: Using bricks + Meta, conditionally show a post listing under 3 different conditions, based on the Current Date…
Pro
Meta Box Group’s Number Sub Field Values Sum

Meta Box Group’s Number Sub Field Values Sum

In the Bricks Facebook group a user asked: We can define a custom function that takes a Meta Box group ID, the sub field ID…
Categories:
Tags:
Pro
Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

How to link a Bricks post-creation form on a post to a newly created post of another post type using Meta Box relationship.
Pro
Dynamic Source for Video Element in Bricks Using Meta Box Post Field

Dynamic Source for Video Element in Bricks Using Meta Box Post Field

This Pro tutorial provides the steps to set the source of a Bricks video element on single CPT pages to be the URL of a…
Categories:
Random Meta Box Image

Random Meta Box Image

Displaying a random image from a set of images from a Meta Box Images Advanced-type field.
Categories:
Tags: