Filtering data
How to add filters to your requests
When you fetch a collection of resources, you can add filters to further refine the results, using the filter[q]
query parameter.
Filters can be combined according to an "AND" logic to send requests that fetch collections of resources meeting all the criteria within a set of multiple filters (see example).
Each single filter query is built like this:
Here, value
stands for the value that the fetched resources have to match, while the predicate
parameter has this format:
Where attributes
is a set of one or more attributes and matcher
represents the condition to be met by the query.
Attributes can be combined according to an OR
logic and can belong to either the fetched resource or to one of its relationships (see example).
List of predicates
You can find here below the complete list of all possible predicates:
Predicate | Description | Example |
---|---|---|
| The attribute is equal to the filter value, not including null values. |
|
| The attribute is equal to the filter value, including null values. |
|
| The attribute is not equal to the filter value, not including null values. |
|
| The attribute is not equal to the filter value, including null values. |
|
| The attribute matches the filter value with "LIKE" operator (works with strings only). |
|
| The attribute does not match the filter value with "LIKE" operator (works with strings only). |
|
| The attribute matches any of the filter values (comma-separated) with "LIKE" operator (works with strings only). |
|
| The attribute matches all of the filter values (comma-separated) with "LIKE" operator (works with strings only). |
|
| The attribute does not match any of the filter values (comma-separated) with "LIKE" operator (works with strings only). |
|
| The attribute matches none of the filter values (comma-separated) with "LIKE" operator (works with strings only). |
|
| The attribute is less than the filter value. |
|
| The attribute is less than or equal to the filter value. |
|
| The attribute is greater than the filter value. |
|
| The attribute is greater than or equal to the filter value. |
|
| The attribute is not null and not empty (works with strings only). |
|
| The attribute is null or empty (works with strings only). |
|
| The attribute is null. |
|
| The attribute is not null. |
|
| The attribute matches any of the filter values (comma-separated), not including null values. |
|
* | The attribute matches any of the filter values (comma-separated), including null values. |
|
| The attribute matches none of the filter values (comma-separated), not including null values. |
|
| The attribute matches none of the filter values (comma-separated), including null values. |
|
| The attribute is less than any of the filter values (comma-separated). |
|
| The attribute is less than or equal to any of the filter values (comma-separated). |
|
| The attribute is greater than any of the filter values (comma-separated). |
|
| The attribute is greater than or qual to any of the filter values (comma-separated). |
|
| The attribute is less than all of the filter values (comma-separated). |
|
| The attribute is less than or equal to all of the filter values (comma-separated). |
|
| The attribute is greater than all of the filter values (comma-separated). |
|
| The attribute is greater or equal to all of the filter values (comma-separated). |
|
| The attribute is equal to none of the filter values (comma-separated). |
|
| The attribute starts with the filter value (comma-separated, works with strings only). |
|
| The attribute does not start with the filter value (comma-separated, works with strings only). |
|
| The attribute starts with any of the filter values (comma-separated, works with strings only). |
|
| The attribute starts with all of the filter values (comma-separated, works with strings only). |
|
| The attribute does not start with any of the filter values (comma-separated, works with strings only). |
|
| The attribute starts with none of the filter values (comma-separated, works with strings only). |
|
| The attribute ends with the filter value (works with strings only). |
|
| The attribute does not end with the filter value (works with strings only). |
|
| The attribute ends with any of the filter values (comma-separated, works with strings only). |
|
| The attribute ends with all of the filter values (comma-separated, works with strings only). |
|
| The attribute does not end with any of the filter values (comma-separated, works with strings only). |
|
| The attribute ends with none of the filter values (comma-separated, works with strings only). |
|
| The attribute contains the filter value (works with strings only). |
|
| The attribute does not contains the filter value (works with strings only). |
|
| The attribute contains any of the filter values (comma-separated, works with strings only). |
|
| The attribute contains all of the filter values (comma-separated, works with strings only). |
|
| The attribute contains none of the filter values (comma-separated, works with strings only). |
|
| The attribute contains a portion of the JSON used as filter value (works with object only). |
|
| The attribute is true. |
|
| The attribute is false. |
|
In case of comma-separated sets of values (e.g. *_in
, *_matches_all
, *_start_any
etc.), pay attention to avoid whitespaces before or after each comma.
Predicates commonly used to filter by string attributes (e.g. *_cont
, *_start
, etc.) don't work when trying to filter by ID.
Filterable fields
Please note that if you try to filter a collection of resources by a non-filterable field you don't get any error from the API which will respond with a 200 OK
status code, returning the full unfiltered list.
Examples
Combining filters
The following request fetches a collection of SKUs that have been created within a specific time range:
Combining filters and attributes
The following request fetches a collection of orders that have been created or updated after a specific date and that haven't been paid yet:
When combining attribute make sure not to mix types (e.g. string and integer).
Using filters that belong to a resource relationship
The following request fetches a collection of SKUs whose name, code, or shipping category name starts with the string "TSHIRT" (as mentioned above, the attributeshipping_category_name
here belongs to a relationship and filters the list of SKUs by the name of the associated shipping category):
*_blank
and *_present
predicates don't work when filtering on related resources fields (e.g. you cannot check the existence of a relationship using/api/customers?filter[q][customer_group_id_present]=true
). You can use *_null
and *_not_null
instead.
Filtering related resources
The following request fetches the list of orders placed by a certain customer after a specific date:
The following request fetches the list of line items of type skus
associated with a specific order:
Last updated