Filter types
Meta filters provide the ability to quickly filter meta fields in your data. Or use REST filters to enable completely custom filtering within any post type’s REST requests. Pass custom query variables, check them in a secondary filter and modify the full database query to search for specific data points.
Meta filters
Since Index provides all filters for taxonomies and universal data types like publish date and status, smart filters are usually created for meta values important to your data structure. All of the heavy lifting is taken care of for you with meta filters and you only need provide your id, label, component, and options. See the Creating filters [LINK] document for full information on creating meta filters.
REST filters
REST filters work similarly to to meta filters, but allow you to control more of the logic with how data is handled in the final query. This requires adding another filter using a built-in WordPress REST hook:
add_filter("rest_{$type}_query", function($args, $request) {
return $args;
}, 10, 2);
The $args
parameter is a WP_Query
arguments array, and the $request
parameter is all query parameters passed into the REST request. Here is a real example of how Index provides a REST filter for featured products in WooCommerce:
// Create a new smart filter for featured product status
add_filter('index_product_filters', function($filters) {
$filters[] = [
'id' => 'wc_featured',
'query' => 'rest_field',
'label' => 'Featured',
'component' => 'radio',
'options' => [
[
'label' => 'Yes',
'value' => 'yes'
], [
'label' => 'No',
'value' => 'no'
]
]
];
});
// Handle querying featured products, which is a taxonomy and not a meta field
add_filter('rest_product_query', function($args, $request) {
if(isset($request['wc_featured'])) {
$args['tax_query'][] = [
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => ['featured'],
'operator' => $request['wc_featured'] === 'yes' ? 'IN' : 'NOT IN'
];
}
return $args;
}, 10, 2);
You can learn more about the rest_{$type}_query
in the WordPress documentation [https://developer.wordpress.org/reference/hooks/rest_this-post_type_query/]