Bricks enables us to build nested query levels of unlimited levels.
Here are a couple of examples:
- Posts Grouped by Categories
- Nested Queries in Bricks – Posts Grouped by Published Years and Categories
Sometimes, you might want to obtain the parent (of any level, not just the immediate parent) query loop’s object and, from this object, the term_id or post id or other data, depending on what is being looped.

There are two ways of getting a parent query loop object in Bricks.
- By element ID.
- Dynamically using a custom function in which we specify the desired level.
Let’s explore these in this Pro tutorial.
By element ID
BricksQuery::get_query_for_element_id( 'rkkphz' )->loop_object
Replace rkkphz with the Bricks element ID. This can be that of any parent on which there is a query loop.
To see the properties of the object:
print( "<pre>" . print_r( BricksQuery::get_query_for_element_id( 'rkkphz' )->loop_object, true ) . "</pre>" );
To get the Term ID of the above object (if it happens to be a term like a category):
echo BricksQuery::get_query_for_element_id( 'rkkphz' )->loop_object->term_id;
Dynamically using a custom function
Add the custom function shared by Jenn Lee at https://itchycode.com/bricks-builder-useful-functions-and-tips/.
<?php
// My own helper function to get looping parent query Id by level
function itchy_get_bricks_looping_parent_query_id_by_level( $level = 1 ) {
global $bricks_loop_query;
if ( empty( $bricks_loop_query ) || $level < 1 ) {
return false;
}
$current_query_id = BricksQuery::is_any_looping();
if ( !$current_query_id ) {
return false;
}
if ( !isset( $bricks_loop_query[ $current_query_id ] ) ) {
return false;
}
$query_ids = array_reverse( array_keys( $bricks_loop_query ) );
if ( !isset( $query_ids[ $level ] )) {
return false;
}
if ( $bricks_loop_query[ $query_ids[ $level ] ]->is_looping ) {
return $query_ids[ $level ];
}
return false;
}
Usage:
BricksQuery::get_loop_object( itchy_get_bricks_looping_parent_query_id_by_level() )
When no argument is supplied, we get the loop object of the immediate parent.
We can pass 2, for example, to get the loop object of 2nd level parent.
BricksQuery::get_loop_object( itchy_get_bricks_looping_parent_query_id_by_level( 2 ) )
To view the object’s properties:
print( "<pre>" . print_r( BricksQuery::get_loop_object( itchy_get_bricks_looping_parent_query_id_by_level() ), true ) . "</pre>" );
To get the Term ID of the above object (if it happens to be a term like a category):
echo BricksQuery::get_loop_object( itchy_get_bricks_looping_parent_query_id_by_level() )->term_id;