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

HappyFiles Bricks Query Loop

HappyFiles Bricks Query Loop

This tutorial explores different ways in which images from WordPress Media Library organized via HappyFiles can be pulled and shown in Bricks using query loop(s).…
Categories:
Random Posts in the Same Category as the Current Single CPT Post in Bricks

Random Posts in the Same Category as the Current Single CPT Post in Bricks

Here’s how query loop can be set in Bricks to show related posts by a taxonomy’s terms
Categories:
Latest Featured Posts First in Bricks Query Loop (No Duplicates)

Latest Featured Posts First in Bricks Query Loop (No Duplicates)

In the Bricks Facebook group a user asks: We can use AI to help unfreeze our brains sometimes or at least have it write 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
Outputting ACF Repeater’s Sub Fields

Outputting ACF Repeater’s Sub Fields

This Pro tutorial provides the code for printing custom HTML after looping through rows of a ACF Repeater (available in ACF Pro) that has a…
Categories:
Pro
Post Views Counter Query Loop in Bricks

Post Views Counter Query Loop in Bricks

How we can output the most viewed posts in your Bricks site in a query loop.
Categories: