bricks_set_query_loop
Overview
Configure a query loop on a Bricks element to repeat it for each result in a WordPress query. Supports four object types: post (WP_Query for posts/pages/CPTs), term (WP_Term_Query for categories/tags), user (WP_User_Query), and api (external REST API endpoints).
This is the primary tool for displaying dynamic content in Bricks — blog grids, product listings, team directories, taxonomy archives, and external API data. The element with hasLoop becomes a template that repeats for each query result.
CRITICAL: Never put hasLoop on a grid container. Use a 2-level structure: parent grid div (layout only) containing a child div (hasLoop) that gets repeated as grid items.
Key Features
When to Use
Creating taxonomy term listings (categories, tags, product categories)
Building user directories or team member grids from WordPress users
Fetching and displaying data from external REST APIs
Setting up archive-style layouts with dynamic content
For post queries: the target post type must exist (built-in or registered via bricks_register_post_type)
For term queries: the taxonomy must exist
For API queries: a publicly accessible REST API endpoint
When NOT to Use
Putting hasLoop directly on a grid container — this creates N separate grids instead of N items in one grid
You need the same query on multiple pages — create a global query with bricks_create_global_query first
Parameters
page_idnumberREQUIREDelement_idstringREQUIREDquery.objectTypestringoptionalpost Values: post, term, user, apiquery.post_typestring | string[]optionalpostquery.posts_per_pagenumberoptional6query.orderbystringoptionaldatequery.orderstringoptionalDESC Values: ASC, DESCquery.tax_queryarrayoptionalquery.meta_queryarrayoptionalquery.exclude_current_postbooleanoptionalquery.taxonomystring | string[]optionalquery.hide_emptybooleanoptionaltruequery.rolestring | string[]optionalquery.api_urlstringoptionalquery.response_pathstringoptionalcontent_areastringoptionalcontent Values: content, header, footerCode Examples
Warning: Undefined array key "example_output" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 12
Correct 2-level grid + loop pattern
The correct way to display query results in a grid. The grid parent has NO hasLoop; the child div inside it gets the query loop. This creates one grid with N items instead of N separate grids.
// Step 1: Add section with 2-level structure
bricks_add_section({
page_id: 123,
section: {
type: "section",
children: [{
type: "container",
children: [{
type: "div",
ref: "posts-grid",
settings: {
_display: "grid",
_gridTemplateColumns: "1fr 1fr 1fr",
"_gridTemplateColumns:tablet_portrait": "1fr 1fr",
"_gridTemplateColumns:mobile_portrait": "1fr",
_gap: "30"
},
children: [{
type: "div",
ref: "loop-item",
settings: {
_display: "flex",
_direction: "column"
},
children: [
{ type: "heading", ref: "card-title", settings: { tag: "h3", text: "Title" }, globalClasses: ["heading-card"] },
{ type: "text-basic", ref: "card-desc", settings: { text: "Excerpt" }, globalClasses: ["text-body"] }
]
}]
}]
}]
}
});
// Step 2: Set query on the CHILD div, NOT the grid parent
bricks_set_query_loop({
page_id: 123,
element_id: "<loop-item-id>",
query: {
objectType: "post",
post_type: "portfolio",
posts_per_page: 6,
orderby: "date",
order: "DESC"
}
});
// Step 3: Set dynamic data on card elements
bricks_set_dynamic_data({ page_id: 123, element_id: "<card-title-id>", setting_key: "text", tag: "bricks_set_query_loop" });
bricks_set_dynamic_data({ page_id: 123, element_id: "<card-desc-id>", setting_key: "text", tag: "Configure a query loop on an element (post, term, user, ACF)" });Warning: Undefined array key "example_output" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 12
Query with taxonomy and meta filters
Filter posts by category and a custom meta field, ordered by a numeric meta value.
bricks_set_query_loop({
page_id: 45,
element_id: "abc123",
query: {
objectType: "post",
post_type: "product",
posts_per_page: 12,
orderby: "meta_value_num",
meta_key: "price",
order: "ASC",
tax_query: [
{ taxonomy: "product_cat", terms: [5, 12], field: "term_id", operator: "IN" }
],
meta_query: [
{ key: "in_stock", value: "1", compare: "=" }
]
}
});Common Mistakes
Warning: Undefined array key "fix_description" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 47
Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-includes/kses.php on line 2018
Warning: Undefined array key "wrong_code" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 48
Warning: Undefined array key "right_code" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 49
Warning: Undefined array key "fix_description" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 47
Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-includes/kses.php on line 2018
Warning: Undefined array key "wrong_code" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 48
Warning: Undefined array key "right_code" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 49
Warning: Undefined array key "fix_description" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 47
Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-includes/kses.php on line 2018
Warning: Undefined array key "wrong_code" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 48
Warning: Undefined array key "right_code" in /var/www/vhosts/mcpbricksbuilder.wecode.swiss/httpdocs/wp-content/themes/bricks/includes/elements/code.php(236) : eval()'d code on line 49
Tips & Warnings
Critical: 2-level structure for grids. Never put hasLoop on a grid container. The correct pattern is: grid parent (layout only, NO hasLoop) → child div (hasLoop, repeated per result) → card template elements.
bricks_set_query_loop overwrites _display and _direction on the target element. If your loop item had specific display settings, use bricks_update_element afterward to restore them.
For reusable queries that appear on multiple pages, create a global query with bricks_create_global_query and reference it via useGlobalQuery in the query object.