-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template Parts: Support REST API meta queries. #21851
Conversation
Size Change: -18.3 kB (2%) Total Size: 817 kB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be handled by properly registering a collection query parameter using rest_wp_template_collection_params
and checking for that value in rest_wp_template_part_query
and formatting the meta_query based on the specific parameter value.
For instance, add a theme
parameter with a type
of string
.
We can't blindly open the posts collection to performing arbitrary meta queries.
What does
What are the things that could go wrong? |
It registers it as a query parameter so that validation can occur and it is documented in the schema for the resource. That is how the REST API is designed to function. This style is more akin to the legacy Meta query parameters are not a public query parameter, see |
335376a
to
9d86825
Compare
Thanks for the clarification @TimothyBJacobs. I've updated the code.
But it still lets unregistered parameters pass through, right? Because it worked without registering it. |
Looks good to me, I'd suggest a small change to make sure we don't stomp on anyone else providing a meta query. function filter_rest_wp_template_part_query( $args, $request ) {
if ( $request['theme'] ) {
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
$meta_query[] = array(
'key' => 'theme',
'value' => $request['theme'],
);
$args['meta_query'] = $meta_query;
}
return $args;
}
Correct, we don't error on unknown parameters. |
@TimothyBJacobs Done 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
Thanks for the quick reviews! |
Description
Template Part blocks rely on meta queries, but the REST API does not support them by default. So, they break down when they look for
wp_template_part
s, and the other query arguments don't narrow it down enough to find the correct post on the first page of results.How to test this?
Verify that inserting template parts works as expected even when you already have a lot of customized template parts.
Types of Changes
Bug Fix: Template Parts now load correctly when you have a lot of
wp_template_part
posts.Checklist: