Opened 6 years ago
Last modified 4 years ago
#47194 new enhancement
Posts endpoint: Enable collection parameters for querying by custom field
Reported by: | coreymckrill | Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | needs-patch |
Focuses: | rest-api | Cc: |
Description
Before the REST API was merged into Core, meta queries were possible via a filter parameter:
posts?filter[meta_key]=foo&filter[meta_value]=bar
This was removed when the API was merged. In a recent Slack discussion @kadamwhite mentioned this was due to the potential for very expensive DB queries, but that improvements to meta registration in subsequent versions of WP may have made this more possible.
There are abundant use cases for being able to query posts via the API based on postmeta values, but I haven't been able to find any other Trac tickets about this. So I'm asking the question: would this be feasible now?
Change History (3)
Note: See
TracTickets for help on using
tickets.
Using
register_meta
as a whitelist does seems like it'd be the way forward.Perhaps something like
register_post_meta( 'post', '_my_meta_key', [ 'show_in_rest' => [ 'allow_querying' => true, ], ] );
We'd need to decide how much customization we want to allow as well. For instance, I imagine the collection param schema would default to the existing schema. Do we want to allow customizing that schema as well? It'd allow for things like only allowing querying by certain values.