How to use a Meta Box Image Advanced field with a Bricks Image Element in a Query?

Something that should be easy is driving me nuts (as I have spent hours on it)…

I can get the Meta Box Image Advanced Field to work using the Bricks Gallery Element but not the image element.

As per “Example: Loop images from Metabox.io Advanced Image Field of current post”

I’ve setup this code snippet. and echoed the $query_vars[‘post_in’] variable and it produces the media ids in an array

Array ( [0] => 120 [1] => 121 )

This does not work with the image element as looking at a normal image field the bricks image element seems to want the file name returned but without the file extension.

So I updated the code snippet to produce this. This is the new array being returned

Array ( [0] => adam-kool-ndN00KmbJ1c-unsplash-3x2-full [1] => quino-al-mBQIfKlvowM-unsplash-3x2-full )

This doesn’t work either.

So how do I create a Bricks query that contains a bricks image element within it that can loop through the data pulling from a Meta Box Image Advanced field? This way if the Image Advanced Field has 6 images the query will produce 6 images from bricks.

Okay I think I figured it out. Gone is the code snippet.

I am using the query editor instead. The key is I also had to set the dynamic data for the image to {post_id}. I had it set to the name of my gallery field originally. Here is the code I have in the query editor.

This code does work but it does cause a PHP error in the Builder UI at the top of the page (Error: Cannot access offset of type string on string). The image with the dynamic data also shows “Error:parseerror”.

// For Meta Box Advanced Image Field
// Note: must use {post_id} in image dynamic data to retrieve image
// See: https://forum.bricksbuilder.io/t/dynamic-slider-with-metabox-image-advanced-custom-field/9332/4

// For Grouped Field
$group = rwmb_meta( 'services_section'); // Group Id
$gallery_images_ids =  $group['gallery']; // Field Id

// For Non group field
// $gallery_images = (array) rwmb_meta('field_id', ['size' => 'full']);

// This is not needed for some reason 
// Originally from: https://academy.bricksbuilder.io/article/filter-bricks-posts-query_vars/
// Get the Images Ids only
// $gallery_images_ids = array_keys($gallery_images);
    
// If no gallery images, set empty string array
$gallery_images_ids = count( $gallery_images_ids ) > 0 ? $gallery_images_ids : [''];

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

This code works great. For me, I DID need to uncomment this line. I was using a non-group field.
$gallery_images_ids = array_keys($gallery_images);

1 Like