GB Query Enhancements includes a number of useful hooks to allow developers to extend and alter the plugin’s functionality.
Hooks Reference
This document lists all custom WordPress hooks (filters) provided by GB Query Enhancements. These hooks allow developers to customize query behavior, modify responses, and extend functionality.
Term Query Hooks
gb_query_enhancements_max_terms
Filter the maximum number of terms returned when “Show All” (-1 or 0) is selected for posts per page. This prevents performance issues from unbounded queries.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$max_terms | int | Maximum terms limit. Default: 150 |
Example:
add_filter( 'gb_query_enhancements_max_terms', function( $max_terms ) {
return 300; // Allow up to 300 terms
} );
gb_query_enhancements_term_query_args
Filter the final term query arguments before the query is executed. This is the main hook for modifying term queries and runs for both frontend rendering and editor previews.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$normalized | array | The sanitized/normalized query arguments |
$args | array | The original unsanitized arguments |
$attributes | array | The block attributes |
$page | int | The current page number |
$context | array | Block context, including termId and postId |
The current sentinel in include, exclude, parent, and child_of resolves before integer sanitization: first to $context['termId'], otherwise to the queried WP_Term on a taxonomy, category, or tag archive. Editor REST requests have no queried archive term, so previews require termId context. Both the REST endpoint and looper data use this same normalization.
Without a current term, the sentinel is removed, and an unresolvable selector yields no terms rather than silently widening the query. Every unresolvable selector normalizes to one shared “match nothing” form: object_ids => [0], with any include key removed. object_ids is the sentinel rather than include because WP_Term_Query guards the two clauses differently. In WordPress 6.5 through 6.8 the include clause is built as implode( ',', wp_parse_id_list( $include ) ) and emitted only if ( ! empty( $inclusions ) ) — the string '0' is empty, so include => [0] silently drops the clause and returns the whole taxonomy. Only 6.9+ emits it unconditionally. The object_ids clause guards on the array (if ( ! empty( $args['object_ids'] ) )) in all of those versions, so object_ids => [0] always produces tr.object_id IN (0) and matches nothing.
That form is applied when: an include list containing current has no valid IDs left (an empty include would otherwise return all terms); parent or child_of is an unresolvable current; or an object_ids list (array or CSV string) whose only entry was an unresolvable current comes back empty (an empty object_ids is no restriction at all). Other valid include or exclude IDs survive whenever the query is still answerable. An unresolved parent or child_of overrides any include list that was also set: the terms being asked for are the ones under a parent that could not be found, so returning that list unfiltered would answer a different question. Those args cannot simply be dropped, because that would return the whole taxonomy, and they cannot become 0 either, since explicit numeric parent: 0 legitimately selects root terms. object_ids: ['current'] continues to refer to the current post, independently of the current term.
The sentinel is applied once, after all other normalization, so a filter-visible object_ids value set elsewhere in the same args cannot overwrite it.
The editor always offers Current Term for Parent term and Child of, allowing archive-template configuration. Include/exclude offer it only inside term loops to avoid an unresolved choice in ordinary queries; saved values still support the archive fallback. Term-query selectors store 'current' or string IDs (arrays for include/exclude), never option objects.
Example:
add_filter( 'gb_query_enhancements_term_query_args', function( $normalized, $args, $attributes, $page ) {
// Only show terms with posts
$normalized['hide_empty'] = true;
// Add custom ordering based on block attribute
if ( ! empty( $attributes['customOrder'] ) ) {
$normalized['orderby'] = $attributes['customOrder'];
}
return $normalized;
}, 10, 4 );
gb_query_enhancements_term_query_data
Filter the full term query data array after the query has executed. Use this to modify results (reorder, remove, inject synthetic items), override pagination values, or add custom data.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$query_data | array | The query data array with keys: data (array of terms), no_results (bool), args (array), total (int), max_num_pages (int) |
$attributes | array | The block attributes |
$page | int | The current page number |
Example:
add_filter( 'gb_query_enhancements_term_query_data', function( $query_data, $attributes, $page ) {
// Remove terms that only have posts in draft status
$query_data['data'] = array_filter( $query_data['data'], function( $term ) {
$posts = get_posts( [
'post_status' => 'publish',
'tax_query' => [ [ 'taxonomy' => $term->taxonomy, 'terms' => $term->term_id ] ],
'fields' => 'ids',
'numberposts' => 1,
] );
return ! empty( $posts );
} );
$query_data['total'] = count( $query_data['data'] );
$query_data['no_results'] = empty( $query_data['data'] );
return $query_data;
}, 10, 3 );
gb_query_enhancements_rest_term_query_args
Filter term query arguments specifically for REST API requests (editor previews). Runs after gb_query_enhancements_term_query_args.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$normalized_args | array | The normalized term query arguments |
$request | \WP_REST_Request | The REST request object |
$args | array | The original args passed to the request |
$attributes | array | The attributes passed to the request |
Example:
add_filter( 'gb_query_enhancements_rest_term_query_args', function( $args, $request, $original_args, $attributes ) {
// Limit preview results in editor
$args['number'] = min( $args['number'] ?? 10, 20 );
return $args;
}, 10, 4 );
gb_query_enhancements_rest_terms_response
Filter the REST API response for term queries. Useful for adding custom data to the editor preview.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$response | array | The response data containing terms, total, total_pages, page |
$request | \WP_REST_Request | The REST request object |
$terms | array | The raw terms array |
Example:
add_filter( 'gb_query_enhancements_rest_terms_response', function( $response, $request, $terms ) {
// Add taxonomy info to response
$response['taxonomy_info'] = get_taxonomy( $request->get_param( 'taxonomy' ) );
return $response;
}, 10, 3 );
gb_query_enhancements_term_response_data
Filter individual term data in REST API responses. Called for each term when formatting the response.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$term_data | array | The term data array with keys: term_id, name, slug, taxonomy, description, parent, count |
$term | \WP_Term | The term object |
Example:
add_filter( 'gb_query_enhancements_term_response_data', function( $term_data, $term ) {
// Add term meta to response
$term_data['featured_image'] = get_term_meta( $term->term_id, 'featured_image', true );
$term_data['custom_url'] = get_term_meta( $term->term_id, 'custom_url', true );
return $term_data;
}, 10, 2 );
gb_query_enhancements_term_meta_query
Filter the built meta_query array for term queries. Useful for adding complex meta conditions.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$meta_query | array | The built meta query array |
$meta_params | array | The original meta parameters |
Example:
add_filter( 'gb_query_enhancements_term_meta_query', function( $meta_query, $meta_params ) {
// Add additional meta clause
$meta_query[] = [
'key' => 'is_featured',
'value' => '1',
'compare' => '='
];
return $meta_query;
}, 10, 2 );
gb_query_enhancements_term_loop_item_context
Filter the block context array for each term loop item before the inner block is rendered. Use this to inject additional context values (e.g., precomputed data, ACF fields, related post IDs) into loop items.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$context | array | The block context array with keys: taxonomy, termId, postId, generateblocks/queryType, generateblocks/loopIndex, generateblocks/loopItem |
$item | \WP_Term | The term object for the current loop item |
$args | array | The query args |
$block | \WP_Block | The parent block instance |
$index | int | The current loop index (1-based) |
Example:
add_filter( 'gb_query_enhancements_term_loop_item_context', function( $context, $item, $args, $block, $index ) {
// Attach a custom "featured image" attachment ID to each term's context
$context['termFeaturedImage'] = get_term_meta( $item->term_id, 'featured_image', true );
return $context;
}, 10, 5 );
gb_query_enhancements_term_query_render_loop_items
Filter the rendered HTML content for term query loop items. Called after all items are rendered.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | The rendered HTML content |
$items | array | The term items that were rendered |
$block | \WP_Block | The block instance |
Example:
add_filter( 'gb_query_enhancements_term_query_render_loop_items', function( $content, $items, $block ) {
// Wrap content in custom container
if ( ! empty( $items ) ) {
$content = '<div class="terms-grid">' . $content . '</div>';
}
return $content;
}, 10, 3 );
User Query Hooks
gb_query_enhancements_max_users
Filter the maximum number of users returned when “Show All” (-1 or 0) is selected for posts per page.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$max_users | int | Maximum users limit. Default: 150 |
Example:
add_filter( 'gb_query_enhancements_max_users', function( $max_users ) {
return 50; // Limit to 50 users for performance
} );
gb_query_enhancements_user_query_args
Filter the final user query arguments before the query is executed.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$normalized | array | The sanitized/normalized query arguments |
$args | array | The original unsanitized arguments |
$attributes | array | The block attributes |
$page | int | The current page number |
The current sentinel in include and exclude resolves to the current user before integer sanitization. Both keys also accept a comma-separated string as well as an array.
Logged out there is no current user, so current sanitizes away. An include list that ends up empty normalizes to include => [0] rather than being dropped: an empty include is no restriction at all in WP_User_Query, so the block would list every user instead of none. WP_User_Query parses the value first ($include = wp_parse_id_list( $qv['include'] )) and guards the clause on that array (if ( ! empty( $include ) )), identically in WordPress 6.5 through 6.9, so [0] always produces ID IN (0) and matches nothing. Unlike the term include clause — see the sentinel note under gb_query_enhancements_term_query_args, which is why terms use object_ids instead — no version-portable workaround is needed here. The array form does matter, though: the string '0' is empty, so it would be dropped by the earlier ! empty( $qv['include'] ) check.
An unresolvable exclude is left alone. Excluding nobody is the correct answer to “exclude the current user” when there is no current user, and other valid IDs in either list survive.
Example:
add_filter( 'gb_query_enhancements_user_query_args', function( $normalized, $args, $attributes, $page ) {
// Only show users who have published posts
$normalized['has_published_posts'] = true;
// Exclude specific users
$normalized['exclude'] = [ 1 ]; // Exclude admin
return $normalized;
}, 10, 4 );
gb_query_enhancements_user_query_data
Filter the full user query data array after the query has executed. Use this to modify results (reorder, remove, inject synthetic items), override pagination values, or add custom data.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$query_data | array | The query data array with keys: data (array of user objects), no_results (bool), args (array), total (int), max_num_pages (int) |
$attributes | array | The block attributes |
$page | int | The current page number |
Example:
add_filter( 'gb_query_enhancements_user_query_data', function( $query_data, $attributes, $page ) {
// Sort users by custom priority meta
usort( $query_data['data'], function( $a, $b ) {
$priority_a = get_user_meta( $a->ID, 'display_priority', true ) ?: 99;
$priority_b = get_user_meta( $b->ID, 'display_priority', true ) ?: 99;
return $priority_a - $priority_b;
} );
return $query_data;
}, 10, 3 );
gb_query_enhancements_rest_user_query_args
Filter user query arguments specifically for REST API requests (editor previews).
Parameters:
| Parameter | Type | Description |
|---|---|---|
$normalized_args | array | The normalized user query arguments |
$request | \WP_REST_Request | The REST request object |
$args | array | The original args passed to the request |
$attributes | array | The attributes passed to the request |
Example:
add_filter( 'gb_query_enhancements_rest_user_query_args', function( $args, $request, $original_args, $attributes ) {
// Limit to specific roles in editor preview
if ( empty( $args['role'] ) ) {
$args['role__in'] = [ 'author', 'editor', 'administrator' ];
}
return $args;
}, 10, 4 );
gb_query_enhancements_rest_users_response
Filter the REST API response for user queries.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$response | array | The response data containing users, total, total_pages, page |
$request | \WP_REST_Request | The REST request object |
$users | array | The raw users array |
Example:
add_filter( 'gb_query_enhancements_rest_users_response', function( $response, $request, $users ) {
// Add available roles to response
$response['available_roles'] = wp_roles()->get_names();
return $response;
}, 10, 3 );
gb_query_enhancements_user_response_data
Filter individual user data in REST API responses.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$user_data | array | The user data array with keys: ID, user_login, user_nicename, user_email, user_url, user_registered, display_name |
$user | \WP_User | The user object |
Example:
add_filter( 'gb_query_enhancements_user_response_data', function( $user_data, $user ) {
// Add user meta
$user_data['bio'] = get_user_meta( $user->ID, 'description', true );
$user_data['avatar_url'] = get_avatar_url( $user->ID );
$user_data['post_count'] = count_user_posts( $user->ID );
return $user_data;
}, 10, 2 );
gb_query_enhancements_user_meta_query
Filter the built meta_query array for user queries.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$meta_query | array | The built meta query array |
$meta_params | array | The original meta parameters |
Example:
add_filter( 'gb_query_enhancements_user_meta_query', function( $meta_query, $meta_params ) {
// Only show verified users
$meta_query[] = [
'key' => 'verified',
'value' => '1',
'compare' => '='
];
return $meta_query;
}, 10, 2 );
gb_query_enhancements_user_loop_item_context
Filter the block context array for each user loop item before the inner block is rendered. Use this to inject additional context values into loop items.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$context | array | The block context array with keys: userId, postId, generateblocks/queryType, generateblocks/loopIndex, generateblocks/loopItem |
$item | stdClass | The user data object for the current loop item |
$args | array | The query args |
$block | \WP_Block | The parent block instance |
$index | int | The current loop index (1-based) |
Example:
add_filter( 'gb_query_enhancements_user_loop_item_context', function( $context, $item, $args, $block, $index ) {
// Add the user's latest post ID to context
$latest = get_posts( [
'author' => $item->ID,
'numberposts' => 1,
'fields' => 'ids',
] );
$context['userLatestPostId'] = $latest[0] ?? 0;
return $context;
}, 10, 5 );
gb_query_enhancements_user_query_render_loop_items
Filter the rendered HTML content for user query loop items.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | The rendered HTML content |
$items | array | The user items that were rendered |
$block | \WP_Block | The block instance |
Example:
add_filter( 'gb_query_enhancements_user_query_render_loop_items', function( $content, $items, $block ) {
// Add schema markup wrapper
if ( ! empty( $items ) ) {
$content = '<div itemscope itemtype="https://schema.org/ItemList">' . $content . '</div>';
}
return $content;
}, 10, 3 );
WooCommerce Query Hooks
gb_query_enhancements_wc_product_query_args
Filter the final WooCommerce product query arguments before the query is executed.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$normalized | array | The sanitized/normalized query arguments |
$args | array | The original unsanitized arguments |
$attributes | array | The block attributes |
$page | int | The current page number |
$context | array | Block context |
Example:
add_filter( 'gb_query_enhancements_wc_product_query_args', function( $normalized, $args, $attributes, $page, $context ) {
// Never show products from the clearance category.
$normalized['category'] = array_diff( (array) ( $normalized['category'] ?? [] ), [ 'clearance' ] );
return $normalized;
}, 10, 5 );
Reserved key:
gbqe_custom_orderby. When the query’sorderbyis a custom value registered throughgb_query_enhancements_wc_orderby_meta_maprather than a built-in one, normalization addsgbqe_custom_orderby => true.WooCommerce_Query::remap_product_orderby()reads it back to tell this plugin’s queries apart from every otherwc_get_products()call on the site, so that an orderby which fails to map falls back todatewithout disturbing WooCommerce’s own queries. Leave it in place — removing it silently disables that guard.It is not stripped on the way to the database.
WC_Data_Store_WP::get_wp_query_args()copies a query var it has no mapping for straight into the WP_Query args, so the key is also visible to thewoocommerce_get_wp_query_argsfilter.WP_Queryignores query vars it does not recognise, so it has no effect on the SQL.
gb_query_enhancements_wc_products_interactivity_context
Filter the woocommerce/products Interactivity context emitted on each WooCommerce loop item wrapper.
WooCommerce’s interactive product blocks — Add to Cart + Options and its inner blocks — resolve the product they act on from the shared woocommerce/products Interactivity store, which reads a productId out of the Interactivity context supplied by an ancestor element. WooCommerce’s own Product Template block puts that on each product <li>; WooCommerce_Query::render_loop_items() puts the equivalent data-wp-context (plus the data-wp-interactive the client runtime needs in order to hydrate the element at all) on each .gb-loop-item wrapper it renders. Without it those blocks render correct-looking markup that is completely inert on the front end.
Existing wrapper contexts and their interactive namespaces are preserved. On WordPress 6.9+, an additional data-wp-context---gb-query-enhancements-products directive supplies the product context alongside an existing one. On WordPress 6.5–6.8, an item with an existing context is left untouched, including skipping the store load, because those versions cannot process multiple context directives on the same element.
The context only names a product; putting its data in state.products is a separate, opt-in step — see gb_query_enhancements_wc_load_products_store below. Blocks that read the store need both; Add to Cart + Options needs only the context.
The filtered return value must be a non-empty array; returning an empty array (or a non-array) skips the context for that item entirely, leaving the wrapper untouched — and skips the store load with it.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$product_context | array | The context array emitted on the loop item wrapper. Built-in: productId => <product or variation ID>, variationId => null |
$product_id | int | The product (or variation) ID for this loop item — the same value passed as the item’s postId block context |
$item | object | The loop item data object, so callbacks can read product data without a second lookup |
Example:
add_filter( 'gb_query_enhancements_wc_products_interactivity_context', function( $context, $product_id, $item ) {
// Leave the context off items the store cannot act on anyway.
if ( 'external' === ( $item->type ?? '' ) ) {
return [];
}
return $context;
}, 10, 3 );
Note: in variations mode productId is the variation. When the query returns variations rather than parent products, each item’s productId is the variation’s own ID and variationId stays null — not the parent with a variationId. WooCommerce’s Add to Cart + Options block hydrates the shared store from whatever ID arrives as block context, so products[ variation_id ] is the only entry that exists for such an item; productVariations is populated for variable parents only, which means a variationId would resolve to null. The rendered result is the same either way: the variation’s own add-to-cart form, with its attributes pre-selected.
gb_query_enhancements_wc_load_products_store
Filter whether to load a WooCommerce loop item’s product into WooCommerce’s shared woocommerce/products Interactivity store.
The context emitted on each loop item names a product; it does not supply its data. wc_interactivity_api_load_product() is what puts the product’s Store API payload in state.products, where blocks that read product data client-side look for it. That call is an internal Store API dispatch per product — roughly 3 ms each, 48.3 ms for a 16-product loop — so it is off by default and gated behind the gbqe_wc_load_products_store option. This filter is applied to the option value and has the final say over it, so a site can enable the load in code, or decide per item.
The tradeoff. Leave it off and a large catalogue loop renders at full speed, but Product SKU, Stock Indicator, Product Specifications and Variation Description go blank on variable products, where WooCommerce binds them to state.productInContext so the value can follow a variation pick. Simple products print their value server-side and are unaffected either way, and Add to Cart + Options loads its own product, so it works with the flag off. Turn it on only when a loop item pairs one of those reading blocks with products that can be variable — or filter it down to exactly those items, which costs nothing on the rest.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$enabled | bool | Whether to load the product. Defaults to the gbqe_wc_load_products_store option ('1'/'0', default '0') |
$product_id | int | The product (or variation) ID about to be loaded — the productId actually emitted in the context |
$item | object | The loop item data object, so the decision can be made from data already in hand |
Example:
add_filter( 'gb_query_enhancements_wc_load_products_store', function( $enabled, $product_id, $item ) {
// Only variable products read the store, so don't pay for the rest.
return 'variable' === ( $item->type ?? '' );
}, 10, 3 );
See docs/woocommerce-interactivity-store.md for the full picture.
gb_query_enhancements_wc_orderby_meta_map
Filter the map of computed orderby values to the postmeta key they sort on. WooCommerce_Query::remap_product_orderby() runs at priority 10 on woocommerce_product_data_store_cpt_get_products_query and rewrites values like 'popularity' (WooCommerce has no native WP_Query support for it) onto a real meta key ordered with meta_value_num. The map is also read while query args are normalized, so keys you add here pass the orderby allow-list and reach the remapping step. This filter is the sanctioned seam for a site to repoint an existing value at its own precomputed key, or to register an entirely new orderby value, without a second callback racing that priority-10 handler on the same WooCommerce filter.
For an orderby value outside the built-in allow-list, the filter also runs during normalization so a valid custom key can pass validation. That call receives WC_Product_Query vars built from the normalized arguments and candidate orderby, with WooCommerce defaults applied; it does not execute a product query. WooCommerce calls the filter again during final remapping with the query vars it supplies to its product data-store callback; those vars can omit or transform values from the earlier normalized set. The return value must be an array of string => string; orderby keys must be non-empty, unchanged by sanitize_key(), and meta keys must be non-empty and unchanged by sanitize_text_field(). Validation is per entry: an entry that fails is dropped and the rest of the map — the entries you added and the built-ins alike — is kept, so a typo in one key cannot quietly send price and popularity back to sorting by date. Only a return value that is not an array at all is ignored wholesale, leaving the built-in map in place.
Note that a callback which replaces the map rather than adding to it drops the built-ins by design. Take $map as a parameter and return it modified, as in the example below.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$meta_orderby_map | array | Map of orderby value => meta key. Built-in: price => _price, popularity => total_sales, rating => _wc_average_rating |
$query_vars | array | WooCommerce query vars; custom-key validation gets vars built from normalized arguments, and the remapping call gets the vars supplied to the product data-store callback |
Example:
add_filter( 'gb_query_enhancements_wc_orderby_meta_map', function( $map, $query_vars ) {
// Register a custom sort alongside WooCommerce's built-in orderby values.
$map['recent_sales'] = '_mm_sales_recent';
return $map;
}, 10, 2 );
Set the product query’s orderby to recent_sales to use that sort. To make it selectable in the editor’s Sort by dropdown rather than only in code, add a matching option with the gb-query-enhancements.queryParameters JavaScript filter, which adds an option to the orderby parameter’s selectOptions. Existing values can also be repointed, for example by assigning '_mm_sales_recent' to $map['popularity'].
The map must answer both calls. The filter runs once during normalization and again during remapping, and those two calls receive differently shaped query vars — the first still carries WooCommerce names such as status, while by the second WooCommerce has renamed some of them (status becomes post_status). A callback that keys its decision on one of those answers only one call, so the orderby passes validation and is then never mapped. That case now falls back to date explicitly rather than letting WP_Query discard the unknown value, but the sort you asked for is still lost. Add your key unconditionally, or key on something present in both.
Warning: zero-fill is mandatory. meta_key combined with orderby => meta_value_num produces an INNER JOIN, so any product missing that meta key is excluded from the results entirely rather than sorted last. total_sales, _price and _wc_average_rating are safe because WooCommerce keeps them synced/defaulted on every product, but a custom key is not — the site’s own code must write it (including 0) to every product it wants included, or a “best sellers” shelf asking for 10 products can silently render 3. Do not gate this with a meta comparison (e.g. > 0) either: a clause added only so orderby has something to reference must impose no filtering condition, or ranking quietly becomes eligibility.
Warning: window boundaries must be end-of-today, not “now”. If the custom meta key is computed over a rolling window (e.g. “sales in the last 30 days”), anchor both ends of the window to calendar days — end-of-today, not the current timestamp — and use calendar-day arithmetic rather than N * DAY_IN_SECONDS. A boundary that moves every second can’t be asserted in a test or reproduced from a snapshot, and N * 86400 drifts off local midnight across a DST transition.
Priority-20 alternative: a site can also skip this filter entirely and hook woocommerce_product_data_store_cpt_get_products_query directly at priority 20 (after GBQE’s priority-10 remap_product_orderby()), swapping $wp_query_args['meta_key'] itself. That pattern still works and is not being removed, but it means reaching into and overwriting GBQE’s own output on a shared WooCommerce filter; gb_query_enhancements_wc_orderby_meta_map is the sanctioned seam and should be preferred for this use case.
Dynamic Tags Hooks
gb_query_enhancements_dynamic_tag_output_{$tag}
Filter the output of a specific dynamic tag. The dynamic portion of the hook name, $tag, refers to the tag name.
Available tags:
- Term:
term_title,term_description,term_archive_url,term_count - User:
user_display_name,user_email,user_bio,user_avatar_url,user_url,user_posts_url,user_post_count - WooCommerce product:
product_title,product_url,product_price(optiontype:''display /regular/sale),product_on_sale,product_sku,product_stock(optionfield:''status /quantity),product_rating,product_review_count,product_image(optionsource:''featured /gallery, plusposition),product_category_image_url,product_attribute,product_dimensions,product_weight,product_variation_description,product_add_to_cart_url,product_add_to_cart_text,product_options,product_option
Parameters:
| Parameter | Type | Description |
|---|---|---|
$output | string | The dynamic tag output |
$options | array | The dynamic tag options |
$instance | \WP_Block | The block instance containing the tag |
Examples:
// Truncate long term descriptions to 100 characters
add_filter( 'gb_query_enhancements_dynamic_tag_output_term_description', function( $output, $options, $instance ) {
if ( strlen( $output ) > 100 ) {
$output = mb_substr( $output, 0, 100 ) . '…';
}
return $output;
}, 10, 3 );
// Output user email as a mailto link
add_filter( 'gb_query_enhancements_dynamic_tag_output_user_email', function( $output, $options, $instance ) {
if ( ! empty( $output ) && is_email( $output ) ) {
$output = sprintf( '<a href="mailto:%s">%s</a>', esc_attr( $output ), esc_html( $output ) );
}
return $output;
}, 10, 3 );
// Append a role badge after user display name
add_filter( 'gb_query_enhancements_dynamic_tag_output_user_display_name', function( $output, $options, $instance ) {
$loop_item = $instance->context['generateblocks/loopItem'] ?? null;
$roles = is_object( $loop_item ) && isset( $loop_item->roles ) ? $loop_item->roles : [];
if ( in_array( 'administrator', $roles, true ) ) {
$output .= ' <span class="role-badge role-admin">Admin</span>';
}
return $output;
}, 10, 3 );
gb_query_enhancements_star_rating_css
Filter the CSS for the built-in SVG star rating used by the product_rating dynamic tag when Stars output is set to Built-in (use_wc_output = false, the default). The built-in stars are SVG shapes masked over currentColor, so they inherit the block’s text color out of the box and render anywhere — no WooCommerce stylesheet required.
The CSS is enqueued as an inline style under the handle gbqe-star-rating, printed once per page (deduplicated by WordPress) only when at least one built-in rating renders. The same filtered CSS is injected into the block editor canvas, so editor previews stay in sync with the front end. Return an empty string to suppress the built-in styles entirely (e.g. to supply your own stylesheet).
Timing: the CSS is captured into the inline style the first time a built-in rating renders on the page. Add this filter early (e.g. on
initorplugins_loaded) so it is in place before that first render.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$css | string | The default star-rating CSS (all rules scoped under .gbqe-star-rating) |
Examples:
// Recolor and resize the built-in stars by appending overrides.
add_filter( 'gb_query_enhancements_star_rating_css', function( $css ) {
return $css . '.gbqe-star-rating{color:#ffb800;width:6em}';
} );
// Suppress the built-in styles to ship your own stylesheet instead.
add_filter( 'gb_query_enhancements_star_rating_css', '__return_empty_string' );
gb_query_enhancements_supported_core_blocks
Filter the list of core blocks that receive dynamic tag replacements and loop context injection. This filters the saved setting value from the Settings > GB Query Enhancements > Core Block Support page. The default is an empty array — no core blocks are enabled until explicitly checked in settings.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$blocks | array | Array of core block names saved in settings (default: []) |
Example:
// Programmatically enable core/paragraph regardless of the saved setting
add_filter( 'gb_query_enhancements_supported_core_blocks', function( $blocks ) {
if ( ! in_array( 'core/paragraph', $blocks, true ) ) {
$blocks[] = 'core/paragraph';
}
return $blocks;
} );
gb_query_enhancements_context_keys
Filter the context keys passed through from parent blocks to supported core blocks during rendering. This is the companion to gb_query_enhancements_supported_core_blocks — if you add custom context via the loop item context filters, you’ll need to add the corresponding keys here for them to be available in core blocks.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$keys | array | Array of context key strings |
Example:
add_filter( 'gb_query_enhancements_context_keys', function( $keys ) {
// Pass custom context keys through to core blocks
$keys[] = 'termFeaturedImage';
$keys[] = 'userLatestPostId';
return $keys;
} );
JavaScript Hooks
These run in the block editor via @wordpress/hooks. Register handlers with wp.hooks.addFilter() (or import { addFilter } from '@wordpress/hooks') from a script enqueued in the editor.
gb-query-enhancements.woocommerce.presets
Filter the list of WooCommerce query presets shown in the Preset dropdown of the Query block’s WooCommerce controls. Use it to add, modify, reorder, or remove presets. Presets are a convenience that auto-fill one or more query parameters; reserve them for meaningful combinations of parameters (a single-setting filter is better surfaced as its own control).
Parameters:
| Parameter | Type | Description |
|---|---|---|
presets | Array | The default presets for the active mode. Each preset is { value: string, label: string, params: Object }. |
args | Object | Context: { isVariations: boolean }. isVariations is true when querying product variations (which cannot order by popularity/rating). |
Preset shape:
value— unique string key for the option.label— human-readable dropdown label.params— object of query parameters to apply. Keys must be validWooCommerce_Queryparameters, otherwise they are ignored server-side. Supported keys include:type,parent_product,status,featured,on_sale,stock_status(instock|outofstock|onbackorder),category,tag,sku,visibility(visible|catalog|search|hidden),shipping_class,include,exclude,min_price,max_price,orderby(date|title|price|popularity|rating|menu_order|rand|id|include|meta_value|meta_value_num),order(ASC|DESC),offset,wc_meta_query.
Variation caveat: when
isVariationsistrue,orderbycannot bepopularityorrating.
includeappears twice above: as a parameter it is a list of product IDs to restrict the query to; as anorderbyvalue it sorts by the order those IDs were given.include/excludeaccept an array of IDs or a comma-separated string.
An
includethat sanitizes away to nothing returns no products. A preset withinclude: 'abc'named a specific set of products, so it normalizes to[0]— WooCommerce mapsincludetopost__in, which WP_Query guards on the array, soID IN (0)matches nothing. Dropping the key would render the whole catalogue instead. An all-invalidexcludeis dropped, because excluding nothing is what it asked for.
Example — add an “On Backorder” preset for products only:
import { addFilter } from '@wordpress/hooks';
addFilter(
'gb-query-enhancements.woocommerce.presets',
'my-plugin/woocommerce/add-backorder-preset',
( presets, { isVariations } ) => {
if ( isVariations ) {
return presets;
}
return [
...presets,
{
value: 'on_backorder',
label: 'On Backorder',
params: { stock_status: [ 'onbackorder' ] },
},
];
}
);
gb-query-enhancements.queryParameters
Filter the parameter definitions a query type’s inspector renders. Use it to add a parameter, remove one, relabel it, change its default, reorder the list, or extend a select control’s options.
Applied inside each parameter getter rather than in the components that call them, so every consumer gets the filtered list. Applied at call time rather than module load — the getters run during render — so a filter registered by another editor script is picked up regardless of script load order, the same way the presets filter above works.
Parameters:
| Parameter | Type | Description |
|---|---|---|
parameters | Array | The built-in parameter definitions for this query type, in render order. |
args | Object | Context: { queryType: string }, plus isVariations for WooCommerce. |
Second argument:
queryType— one of'post','term','user','woocommerce'. Always present. Check it before touching the list, or a change meant for one query type lands on all four.isVariations—boolean, present only whenqueryTypeis'woocommerce'.truewhen querying product variations (which cannot order bypopularity/rating),falsefor products.
Parameter shape: each entry is the object the inspector renders — id, type, default, label, usually description and group, and selectOptions for select/toggleGroup types. Mutating the array you are handed is not safe; return a new one, as both examples below do.
Example — expose a PHP-registered recent_sales sort in the Order by dropdown:
The dropdown is a fixed list, so an orderby value registered in PHP with gb_query_enhancements_wc_orderby_meta_map can be queried but not chosen in the editor. This is how a site exposes one.
import { addFilter } from '@wordpress/hooks';
addFilter(
'gb-query-enhancements.queryParameters',
'my-plugin/add-recent-sales-orderby',
( parameters, { queryType, isVariations } ) => {
if ( 'woocommerce' !== queryType || isVariations ) {
return parameters;
}
return parameters.map( ( parameter ) =>
'orderby' === parameter.id
? {
...parameter,
selectOptions: [
...parameter.selectOptions,
{
value: 'recent_sales',
label: 'Recent sales',
},
],
}
: parameter
);
}
);
The value must also be registered in PHP, either as one of the built-in orderby values or through gb_query_enhancements_wc_orderby_meta_map; anything else is normalized away to date server-side. Adding an option here does not by itself make a sort work:
add_filter( 'gb_query_enhancements_wc_orderby_meta_map', function( $map ) {
$map['recent_sales'] = '_mm_sales_recent';
return $map;
} );
Example — hide a parameter:
import { addFilter } from '@wordpress/hooks';
addFilter(
'gb-query-enhancements.queryParameters',
'my-plugin/hide-term-hide-empty',
( parameters, { queryType } ) => {
if ( 'term' !== queryType ) {
return parameters;
}
return parameters.filter(
( parameter ) => 'hide_empty' !== parameter.id
);
}
);
Removing a parameter only takes it out of the inspector. It does not stop the value being honoured if a saved query already carries it, and it does not sanitize anything server-side — the PHP filters are where a value can actually be refused.
Hook Execution Order
Understanding when hooks fire helps you choose the right one:
Frontend Rendering
gb_query_enhancements_term_query_args/gb_query_enhancements_user_query_argsgb_query_enhancements_term_meta_query/gb_query_enhancements_user_meta_query(if meta query used)gb_query_enhancements_term_query_data/gb_query_enhancements_user_query_datagb_query_enhancements_supported_core_blocks/gb_query_enhancements_context_keys(during block rendering)gb_query_enhancements_term_loop_item_context/gb_query_enhancements_user_loop_item_context(per item)gb_query_enhancements_dynamic_tag_output_{$tag}(per dynamic tag)gb_query_enhancements_term_query_render_loop_items/gb_query_enhancements_user_query_render_loop_items
Editor Preview (REST API)
gb_query_enhancements_term_query_args/gb_query_enhancements_user_query_argsgb_query_enhancements_rest_term_query_args/gb_query_enhancements_rest_user_query_argsgb_query_enhancements_term_response_data/gb_query_enhancements_user_response_data(per item)gb_query_enhancements_rest_terms_response/gb_query_enhancements_rest_users_response