bricks_create_custom_query

Phase 4AdvancedComplexity: 8/10
Register a PHP custom query handler

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

Custom Data Sources
Query any data source — custom DB tables, external APIs, CSV files, JSON files, or dynamically computed data. Not limited to WordPress post types.
Query Type Integration
The handler appears as a selectable query type in the Bricks editor dropdown, fully integrated with the UI.
Loop Object Setup
Optional loop_object_function allows custom setup per iteration, controlling how dynamic data tags resolve for each item.

When to Use

Querying data from custom database tables that are not WordPress post types
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
Prerequisites
PHP knowledge for writing the query_function code body
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 standard WordPress posts, pages, or CPTs — use bricks_set_query_loop with objectType: "post"
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

4 Total Parameters3 Required1 Optional
namestringREQUIRED
Unique query handler name/slug (lowercase, underscores). E.g., "api_products", "custom_table_data", "csv_import".
labelstringREQUIRED
Display label shown in the Bricks query type dropdown (e.g., "API Products", "Custom Table", "CSV Data").
query_functionstringREQUIRED
PHP code body that returns an array of results. Has access to $query_args variable containing the query settings. Must return an array.
loop_object_functionstringoptional
PHP code body to set up each loop iteration. Has access to $loop_item (current item) and $loop_index (current index). Used to set global post or custom context for dynamic data tags.

Code 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.

JSON
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
The query_function PHP code must return an array. Returning null, false, or a single object will cause the query loop to fail. Always ensure the return value is an array, even if empty.

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
Always use $wpdb->prepare() when building SQL queries from $query_args to prevent SQL injection. Never concatenate user input directly into query strings.

Tips & Warnings

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.

Return Values

FieldTypeDescription
successbooleanWhether the query handler was successfully registered.
namestringThe registered query handler name/slug.
file_pathstringPath to the generated PHP file in the child theme.

Related Tools

Technical Details

Tool ID
bricks_create_custom_query
API Endpoint
/bricks-mcp/v1/custom-elements
HTTP Method
POST
Namespace
content-queries
Source File
content/custom-queries.ts
Version
1.0
Min Bricks Version
1.9
Requires Auth
Yes

Changelog

v1.0
Initial release
20250101