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
1 Answer
Reset to default 2As 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
版权声明:本文标题:block editor - meta_query with standard getEntityRecords possible? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736836997a1954932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论