The Query ID lets you change the posts query of a widget with PHP code, when the settings in the widget are not enough. You give the query a unique ID, and add a WordPress filter with the same name. The filter gets the query arguments, and you can change them before the posts are loaded.
Query ID is a Pro feature. In the free version, the field is called Query ID (pro) and you can’t edit it.
Step 1: Set the Query ID
In the posts query section of the widget, for example Items Posts Query, enter a unique name in Query ID. We recommend a name with only letters, numbers and underscores, for example my_query_additions.
Step 2: Add the Filter
Add this code to the functions.php file of your child theme, or to a code snippets plugin. The filter name must be the same as the Query ID:
/**
* $args - the array of query arguments
* $widgetData - the values of the widget settings
*/
function modifyMyQuery($args, $widgetData){
// change $args here
return $args;
}
add_filter("my_query_additions", "modifyMyQuery", 10, 2);
$args
$widgetData
Return value
Example: Show Only Posts With a Featured Image
function modifyMyQuery($args, $widgetData){
if(!isset($args['meta_query']))
$args['meta_query'] = array();
$args['meta_query'][] = array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
);
return $args;
}
add_filter("my_query_additions", "modifyMyQuery", 10, 2);
For more examples, including how to use a widget setting in the filter, see Current Query Posts Custom Filters.
Step 3: Check the Result
Turn on Show Query Debug in the widget. On the page, you see “applying custom args filter: my_query_additions” and the query arguments after your filter. Turn it off before you publish the page.
Where the Query ID Works
To learn about all the query settings, see Advanced Post Query.