2nd Oct '25
/
0 comments

ACF Flexible Content Gallery Field Bricks PHP Query

In the Bricks Facebook group a user asks:

Does anyone have experience accessing an acf gallery with a php query within a flexible content loop?

Normally I use this to access the gallery as a php query:

$acf_images = get_field("gallery"); if (!empty($acf_images)) { return [ 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'orderby' => 'post__in', 'post__in' => $acf_images, 'posts_per_page' => -1, ]; } else { return []; }

I've also tried to incorporate this:

$acf_row = \Bricks\Query::get_loop_object();

to access the subfield within the loop but I can't seem to target it

This Pro tutorial walks you through setting up a ACF Flexible Content field with a couple of layouts (Text - Images and Images - Text) where the Images part uses Gallery-type fields and outputting the galleries' images using PHP query loops.

The magic sauce is the Bricks' render_tag() function.

Note: No custom code is needed to display the gallery images using a 'Image Gallery' element. This tutorial is for when you want more control on the layout via a query loop instead of using the Image Gallery element.

Step 1

Create a field group for your post type having a Flexible Content field.

Step 2

Edit a Page (or a post of your post type) and populate the field.

Step 3

Let's create a custom function that takes a field name and returns an array Bricks' rendered image IDs in the field value.

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

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
Posts from Random Categories in Bricks

Posts from Random Categories in Bricks

Updated on 31 Jul 2023 In BricksLabs Facebook group a user asked: How would you query 3 WordPress posts from 3 different categories with Brick…
Pro
Post ID Query Variable-based Post Output in Bricks

Post ID Query Variable-based Post Output in Bricks

How to query a single post of a custom post type using the post ID passed to a page via a URL parameter.
Pro
Configuring Image Gallery dynamic source in Bricks

Configuring Image Gallery dynamic source in Bricks

Let's say you have a custom field created with ACF Pro of the type Gallery called "Property Gallery" for posts of "Property" CPT. In the…
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:
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
Top-level Pages and Children Bricks Query Loop

Top-level Pages and Children Bricks Query Loop

This Pro tutorial for Bricks users can be considered Part 2 or a variation of the earlier Bricks Query Loop for Top-level Parent Pages with…
Categories:
Pro
Merging ACF Gallery Images with Featured Image in Bricks

Merging ACF Gallery Images with Featured Image in Bricks

Updated on 12 Mar 2025 In the Bricks Facebook group a user asks: Merging images from different fields into a single gallery Hi, I have…
Categories: