admin管理员组

文章数量:1134601

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 ).

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
);

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
);
Share Improve this question asked Aug 16, 2023 at 19:05 ymsyms 355 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: block editormetaquery with standard getEntityRecords possible