-
Notifications
You must be signed in to change notification settings - Fork 0
Query Configs
When working with custom queries in WordPress, a common issue arises when certain query arguments like 'post__in' are left empty, potentially leading to unintended results, such as returning all posts. The fix_empty_post_ins method in the Bricks Custom Query library addresses this by ensuring these arguments are set to [0] if they are empty, in line with the WordPress core ticket #28099.
To apply this fix to your custom query, chain the fix_empty_post_ins method when registering your query:
Query_Registry::set(
'unique_query_name', // Query name
'Readable Query Label', // Query label
function(array $args, $query_obj, Query $query) { // Callback for query args
// Your custom logic to construct query arguments
return array_merge($args, [
'post_type' => 'your_post_type',
// Other arguments
]);
}
)->fix_empty_post_ins(true) // Apply the fix for empty 'post__in' typesThis ensures that if any of the specified 'in' arguments are empty, they are set to [0], preventing the query from returning all posts.
This fix applies to all of these parameters: post__in,post_parent__in,category__in,tag__in,author__in,tag_slug__in
By default, each query is set up to support WP Gridbuilder. In your callback, you will see the required arguments.
Query_Registry::set(
'collection_prompts', // Query name
'Collection Prompts', // Query label
function(array $args, $query_obj, Query $query) { // Callback for query args
return array_merge($args, [
'post_type' => 'posts',
]
);
}
)->wpgb(false);By default, each query is set up to have a control field for the number of items to be returned. In your callback, you will see the required arguments.
Query_Registry::set(
'collection_prompts', // Query name
'Collection Prompts', // Query label
function(array $args, $query_obj, Query $query) { // Callback for query args
return array_merge($args, [
'post_type' => 'posts',
]
);
}
)->per_page_control(false);To keep the control but change the label, pass a second parameter: ->per_page_control(true, 'Per page');
To control which site of a multisite network to query the items from, you can enable the multisite control. This will create a select control, to choose from all available sites of the network. It only works in the context of WordPress native queries, not for "Others". If used, queries will be executed in the context of the network site.
Query_Registry::set(
'collection_prompts', // Query name
'Collection Prompts', // Query label
function(array $args, $query_obj, Query $query) { // Callback for query args
return array_merge($args, [
'post_type' => 'posts',
]
);
}
)->multisite_control(true);