WordPress.org

Make WordPress Core

Opened 4 months ago

Last modified 3 months ago

#47194 new enhancement

Posts endpoint: Enable collection parameters for querying by custom field

Reported by: coreymckrill Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
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 (2)

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


3 months ago

#2 @TimothyBlynJacobs
3 months ago

  • Keywords needs-patch added

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.

Note: See TracTickets for help on using tickets.