bricks_create_custom_query
Overview
Register a custom query handler that extends the Bricks query loop system to support data sources beyond WordPress (external APIs, custom database tables, CSV/JSON files, or computed data). The handler appears as a new query type in the Bricks query type dropdown.
The query_function receives $query_args and must return an array of items. An optional loop_object_function sets up each iteration by receiving $loop_item and $loop_index, allowing you to define how dynamic data tags access the loop data.
Key Features
When to Use
Fetching data from external APIs with complex authentication or transformation logic
Reading data from CSV or JSON files and displaying in a Bricks loop
Creating computed/aggregated data loops (e.g., combining data from multiple sources)
When the built-in API object type in bricks_set_query_loop is too limited
Understanding of how Bricks query loops iterate and how dynamic data tags access loop data
The child theme must be active (custom query handlers are stored as PHP files in the child theme)
The data source (API, database table, file) must be accessible from the WordPress server
When NOT to Use
Querying taxonomy terms or users — use the built-in term/user object types
Simple external API queries — try the api objectType in bricks_set_query_loop first
Parameters
namestringREQUIREDlabelstringREQUIREDquery_functionstringREQUIREDloop_object_functionstringoptionalCode 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
Custom query from a database table
Create a query handler that reads from a custom database table and returns results for a Bricks query loop.
bricks_create_query_handler({
name: "custom_events",
label: "Events (Custom Table)",
query_function: `
global $wpdb;
$table = $wpdb->prefix . 'events';
$limit = isset($query_args['posts_per_page']) ? intval($query_args['posts_per_page']) : 10;
$results = $wpdb->get_results(
$wpdb->prepare("SELECT * FROM {$table} WHERE event_date >= %s ORDER BY event_date ASC LIMIT %d", current_time('Y-m-d'), $limit)
);
return $results ?: [];
`,
loop_object_function: `
// Make each event accessible via dynamic data
global $bricks_loop_item;
$bricks_loop_item = $loop_item;
`
});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 1935
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 1935
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
The query_function has access to $query_args which contains all the settings from the Bricks query UI. Use these to make your handler configurable from the editor.
For external APIs, consider caching responses with WordPress transients to avoid hitting rate limits and improve performance.
Test the handler manually by creating a simple query loop after registration to verify data is returned correctly before building the full page layout.