1

in my custom block I'm trying to display post-type-items that in php are selected with a meta query. Since I needed to compare the meta value to a date and I eventually resorted to creating and using a custom rest endpoint for the block (which had its own challenges, includig a bug-like lack of documentation: see https://github.com/WordPress/gutenberg/issues/25388 ).

But I'm wondering if there isn't a simpler method for achieving this?

I understand (see code below) I can extend the standard getEntityRecords query by adding keys and values, but is there by now also a way to actually run a meta query that allows to compare values, using standard query params?

I couldn't find any info in that regard, but maybe someone here is aware of a simpler method for running a meta query than creating a custom rest endpoint.

Thanks!

add_filter(
    'rest_POSTTYPE_query',
    function( $args, $request ) {
        if ( $meta_key = $request->get_param( 'metaKey' ) ) {
            $args['meta_key']   = $meta_key;
            $args['meta_value'] = $request->get_param( 'metaValue' );
        }
        return $args;
    },
    10,
    2
);

1 Answer 1

2

As far as I can tell, that's still not possible. getEntityRecords() uses the REST API, but that doesn't support meta queries by default. You can follow ticket #47194 if you'd like to keep tabs on the request to add support, or contribute to it.

For now, using rest_{posttype}_query seems to be the conventional way to achieve what you want. I'd recommend that over creating a custom endpoint.

Don't forget that you'll need to register the meta if you also want to see it in the response.

4
  • Thanks for the reply and the link! Since you say that rest_{posttype}_query would be the standard way to do this - maybe I'm just lacking the imagination, but how would I include a date comparison in a function filtered by rest_{posttype}_query? Am I wrong in the assumption that the filter will only add meta keys and values to the query (and thus allow to include/exclude records based on specific values but that it won't allow to add comparisons/operations to the query that would in turn allow something like "datekey/datevalue" > "today"?
    – yms
    Aug 16, 2023 at 22:08
  • You should be able to add anything that WP_Query supports. The simple way would be using meta_compare. If you need to add multiple comparisons, you can have your filter callback setup a meta_query array.
    – Ian Dunn
    Aug 16, 2023 at 22:20
  • The names of the parameters in your REST request are arbitrary, so anything will work as long as your filter callback maps them to the appropriate WP_Query parameters. In fact, it's probably a good idea to prefix them with your plugin slug, just in case Core starts allowing meta fields in native requests in the future.
    – Ian Dunn
    Aug 16, 2023 at 22:20
  • Thanks a lot for the additional info =)
    – yms
    Aug 17, 2023 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.