Hey Guys, i have a question regarding queries for MB Group. I have a group with a repeater item i wanna loop. However, that item has a switch “to_display” - if its on, it should be displayed, if not, it shouldnt be. Since Bricks Builder cant natively query group items, i made a custom query. That custom query seems to work, and i get exactly the items i want. However, i cant output any custom data from it.
So, for example, i have a “mb_group_item_heading”, which i have pasted into bricks as {mb_group_item_heading} - but it doesnt output anything (it does output correctly if i go with the native bricks way, but then i am not able to filter anything out).
Do you guys know why that is or how i can resolve that?
/* Add new query type control to query options */
add_filter('bricks/setup/control_options', 'setup_flexible_items_query_control');
function setup_flexible_items_query_control($control_options) {
// Adding a new option in the dropdown
$control_options['queryTypes']['flexible_items_query'] = esc_html__('Flexible Items Query');
return $control_options;
}
/* Modify query results for the custom query type */
add_filter('bricks/query/result', function ($results, $query_obj) {
// Ensure this is the correct query type
if ($query_obj->object_type !== 'flexible_items_query') {
return $results;
}
// Fetch the group field
$group = rwmb_meta('classpack__group'); // Replace with your Meta Box group field key
$items = $group['item'] ?? []; // Extract the items from the group
// Filter the items to include only those with 'is_flexible' set to true
$filtered_items = array_filter($items, function ($item) {
return !empty($item['is_flexible']) && $item['is_flexible'] == 1;
});
// Map the filtered items to a format compatible with Bricks
$results = $filtered_items;
// Debugging: Output the filtered results for inspection
if (defined('WP_DEBUG') && WP_DEBUG) {
Bricks\Helpers::pre_dump($results);
}
return $results; // Return the modified results
}, 10, 2);
/* Setup post data for posts */
add_filter('bricks/query/loop_object', 'setup_post_data_for_flexible_items', 10, 3);
function setup_post_data_for_flexible_items($loop_object, $loop_key, $query_obj) {
// Ensure this is the correct query type
if ($query_obj->object_type !== 'flexible_items_query') {
return $loop_object;
}
global $post;
$post = get_post($loop_object);
setup_postdata($post);
return $
and this is the dump i get (idk why i get it twice)
this is the code i used, and which outposts the correct number of items on the front end, however i dont have access to any variables/dynamic data with it:
/* Add new query type control to query options */
add_filter('bricks/setup/control_options', 'setup_filter_2_query_control');
function setup_filter_2_query_control($control_options) {
/* Adding a new option in the dropdown */
$control_options['queryTypes']['filter_2'] = esc_html__('Filter 2 Query');
return $control_options;
}
/* Run new query if option selected */
add_filter('bricks/query/run', 'run_filter_2_query', 10, 2);
function run_filter_2_query($results, $query_obj) {
if ($query_obj->object_type !== 'filter_2') {
return $results;
}
/* If option is selected, fetch custom data */
$results = fetch_filter_2_items();
return $results;
}
/* Setup post data for Bricks Builder */
add_filter('bricks/query/loop_object', 'setup_post_data_for_filter_2', 10, 3);
function setup_post_data_for_filter_2($loop_object, $loop_key, $query_obj) {
if ($query_obj->object_type !== 'filter_2') {
return $loop_object;
}
global $post;
$post = get_post($loop_object);
setup_postdata($post);
return $loop_object;
}
/* Fetch and filter items for 'Filter 2' */
function fetch_filter_2_items() {
// Fetch the Meta Box group field
$group = rwmb_meta('classpack__group'); // Replace with your field key
$items = $group['item'] ?? []; // Extract the items from the group
// Filter items with 'is_flexible' set to true
$filtered_items = array_filter($items, function($item) {
return !empty($item['is_flexible']) && $item['is_flexible'] == 1;
});
// Map the filtered items to a Bricks-compatible format
return array_map(function($item) {
return (object) [
'id' => $item['link'] ?? uniqid(), // Use 'link' or generate a unique ID
'name' => $item['name'] ?? '',
'subheading' => $item['subheading'] ?? '',
'price' => $item['price'] ?? '',
'price_per' => $item['price_per'] ?? '',
'term' => $item['term'] ?? '',
];
}, $filtered_items);
}
Your second code-block seems to cut off in the middle of the code.
The pre_dump showing up twice is not relevant, it just gets called twice within bricks.
Just to be sure, you changed your fieldname from to_display to is_flexible right?
You are using the wrong filter. You need to change the bricks/query/run not the bricks/query/result filter to setup your custom query-type
So I see you already filtered out the elements where is_flexible is false. We could have done that in the next step via bricks conditions but that’s just preference
I don’t know what you plan to do with the bricks/query/loop_object filter.
From here on out, I would just create a function (or dynamic_data_tag, but that is more advanced) that returns the current loop-object as an array, and then use the array_filter_value functionality dynamic-data tags have, to return each value.
function prefix_get_current_loop_object()
{
$loop_object = Bricks\Query::get_loop_object() ?? '';
return $loop_object;
}
add_filter('bricks/code/echo_function_names', function () {
return [
'prefix_get_current_loop_object',
];
});
How to use in your Text- or Heading-Element: {echo:prefix_get_current_loop_object:array_value|name} or {echo:prefix_get_current_loop_object:array_value|is_flexible} this could be used for conditional displays
Thank you very much, will try that out - curious about the conditional visibility thing. that would have been my first try - so i iterated over everything with a “normal” group query from bricks, and the on the query element itself i set a conditional display to only show when the dynamic date “is_flexible” equals 1. But that then leads to no element showing up at all, do you know why that is?
Thank you - i seem to finally get some data out aswell with that echo function, however, the “{echo:prefix_get_current_loop_object:array_value|name}” does give me the full arry of data - how do i make so it really only gives me the “name” value (what i assume should technically be the case on that statement)
Also, is there no way to reference the dynamic value “dynamically”, without mapping it first in the functions.php and then needing an echo function.
That’s surprising. It’s supposed to return the name value.
The whole array should be returned when you output {echo:prefix_get_current_loop_object}
Maybe you can post what happens when you output the ladder.
honestly I don’t quite understand what you mean with dynamically. Maybe you could make a video or post some screenshots for clarification. Also i don’t use metabox (acf instead), so I’m not so familiar with it.
in that code, the whole object gets returned, with no regard to the passed “:name”, right?
what i mean by the repeater thing etc. shouldnt be related to that. i mean just working with dynamic data in general, right? so when you have a cpt “person” and you have a field “person_name”, normally, when you are in a query in “person” cpt, u can reference that field in bricks with just using {person_name} and the value will adapt to what the name is in the backend, so i was wondering why i need an echo function for this in the first place.
going back to the dynamic visibility: how would that go?
the function should work correctly. It is supposed to return an array. all the values from this array can be accessed inside bricks by useing this by referencing the wanted key of said array like this :array_value|name. In this case we want to access the value of the key “name”.
That’s why we need to write the dynamic data inside bricks like this: {echo:prefix_get_current_loop_object:array_value|name}
the standard bricks dynamic-data-tags don’t “understand” the context of your custom query, that’s why you can’t use them. it would be different if you would query for some posts of a custom post type or term etc.
For dynamic visibility you would just take that function and set a condition for the whole looping-div for {echo:prefix_get_current_loop_object:array_value|is_flexible} == 1
But first of you need to make sure that the function works correctly. I tested it with your array that you provided, and it worked fine for me. So I can’t tell you what the problem is