WAIT: Query loop on Meta Box File Advanced inside a Meta Box Group repeats wrong items after first post

Hello, I’m using Bricks 2 + Meta Box.

I have a custom post type that contains a set of custom fields.

Inside these custom fields, there is a clonable group that contains:

  • Text – the name of a dropdown
  • File Advanced – a list of files that should be shown in the dropdown

Each post has 2 items in this group, and each item has 2–4 files in the File Advanced field.

I have a query loop that iterates through each post in my custom post type β€” this works perfectly.

Inside it, I have another query loop that iterates through the Meta Box group β€” this also seems to work. If I print the Text field, it shows correctly, and if I print the File Advanced field, it displays the correct list of links (<a href="...">File Name</a>, [...]).

The issue starts when I add a new loop to iterate inside the File Advanced field (i.e., over the files).

  • For the first post, everything is correct: I get all items for both dropdowns.
  • For the other posts, the first group item prints correctly (the first dropdown is fine), but the second dropdown prints the items from the first post (instead of its own second items list).

When I debug in PHP and print the post ID and the group iteration, I get:

post-id - iteration-index
154 - 0
154 - 1
155 - 0
156 - 0
155 - 0
156 - 0

So only the first post (154) iterates with indices 0 and 1. Posts 155 and 156 seem to iterate twice but only with index 0.

I tried various things; currently I’m fetching the current loop object with: \Bricks\Query::get_loop_object()

When I print it, the output aligns with the loop index: it’s correct only for post 154. On posts 155 and 156, it iterates twice but always for the first group of items.

If I change the order of the group it is always the new first group that gets printed correctly.

I’m not sure whether I’m making a mistake or if this is a bug. Any hints would be appreciated β€” thanks in advance!

Hi @Zandalus,

can you explain the setup from here on? Possibly with the video, so that I can replicate this part?

Also, can you copy-paste related elements, so I can test locally with the same settings as you?

Thank you,
Matej

Hi Matej!

Thank you for your help :slight_smile:

I have a custom field made with Meta Box


It is a cloneable group with inside a Text field and a File Advanced field that can contains up to 6 files.


I tried with both Save in multiple rows on or off but I do get the same result.


Bricks setup:

Loop 1:

Loop 2:

Loop 3:


Data in the first post with meaningful names to make the error more explicit

Loop for the first Island:


Loop for the second Island:



The second dropdown has the incorrect items.


I also did create a video of my setup

And you can check a live example here: https://bricks.staging.gamethinking.it

I made countless tests, but I recreated three of them in this live site and here is the code for the internal loop for the files in the File Advanced field.

Test 1

$data = get_bricks_parent_loop_object();

if($data)
{
	$file_ids = $data['files_list'];
}

if (empty($file_ids)) {
    return [
        'post_type'      => 'attachment',
        'post_status'    => 'inherit',
        'post__in'       => [0],
        'posts_per_page' => -1,
        'no_found_rows'  => true,
    ];
}

return [
    'post_type'      => 'attachment',
    'post_status'    => 'inherit',
    'post__in'       => $file_ids,
    'orderby'        => 'post__in',
    'posts_per_page' => -1,
    'no_found_rows'  => true,
];
function get_bricks_parent_loop_object($level = 1) {
    global $bricks_loop_query;
    
    if (empty($bricks_loop_query)) 
    {
        error_log('No bricks loop query');
        return false;
    }

    $query_ids = array_reverse(array_keys($bricks_loop_query));
    if (!isset($query_ids[$level])) 
    {
        error_log('No reverse query');
        return false;
    }

    $parent_query_id = $query_ids[$level];
    
    return \Bricks\Query::get_loop_object($parent_query_id);
}

Test 2

$parent_id = get_the_ID();
$current_index = get_bricks_parent_loop_index();

error_log('$parent_id ' . $parent_id . ' idx ' . $current_index);

$group_data = rwmb_meta('materiale_didattico_group', [], $parent_id);

if (isset($group_data[$current_index]['files_list'])) {
    $file_ids = $group_data[$current_index]['files_list'];
  
} else {
    $file_ids = [];
}

/*
error_log($parent_id . ' ' . $current_index . ' file ids: ' . implode(',', $file_ids));*/

if (empty($file_ids)) {
    return [
        'post_type'      => 'attachment',
        'post_status'    => 'inherit',
        'post__in'       => [0],
        'posts_per_page' => -1,
        'no_found_rows'  => true,
    ];
}

return [
    'post_type'      => 'attachment',
    'post_status'    => 'inherit',
    'post__in'       => $file_ids,
    'orderby'        => 'post__in',
    'posts_per_page' => -1,
    'no_found_rows'  => true,
];
function get_bricks_parent_loop_index() {
    global $bricks_loop_query;
    $ids = array_reverse(array_keys($bricks_loop_query)); 
    $parent_qid = $ids[1] ?? null;
    return $parent_qid !== null ? \Bricks\Query::get_loop_index($parent_qid) : null;
}

Test 3 - Using Bricks Extras for the current loop index

$parent_id = get_the_ID();
$current_index = '{x_parent_loop_index}';

$group_data = rwmb_meta('materiale_didattico_group', [], $parent_id);

if (isset($group_data[$current_index]['files_list'])) {
    $file_ids = $group_data[$current_index]['files_list'];
  
} else {
    $file_ids = [];
}

/*
error_log($parent_id . ' ' . $current_index . ' file ids: ' . implode(',', $file_ids));*/

if (empty($file_ids)) {
    return [
        'post_type'      => 'attachment',
        'post_status'    => 'inherit',
        'post__in'       => [0],
        'posts_per_page' => -1,
        'no_found_rows'  => true,
    ];
}

return [
    'post_type'      => 'attachment',
    'post_status'    => 'inherit',
    'post__in'       => $file_ids,
    'orderby'        => 'post__in',
    'posts_per_page' => -1,
    'no_found_rows'  => true,
];