admin管理员组文章数量:1125429
How can I retrieve posts, using backbone, that match a particular custom field value?
My custom fields are created through ACF.
Something like this doesn't work:
var data = {
offset: 0,
_embed: 1,
per_page: 100,
_fields: "_embedded,_links,id,acf,title",
meta_key: "status",
meta_value: "new"
};
postsCollection
.fetch({ data: data })
How can I retrieve posts, using backbone, that match a particular custom field value?
My custom fields are created through ACF.
Something like this doesn't work:
var data = {
offset: 0,
_embed: 1,
per_page: 100,
_fields: "_embedded,_links,id,acf,title",
meta_key: "status",
meta_value: "new"
};
postsCollection
.fetch({ data: data })
Share
Improve this question
asked Feb 6, 2024 at 20:54
MastaBabaMastaBaba
3113 silver badges12 bronze badges
2
|
1 Answer
Reset to default 1The REST API endpoint "List Posts" (e.g. GET /wp/v2/posts
) does not support filtering the posts by a post meta (or custom field), therefore passing post meta parameters like meta_key
and meta_value
will by default do nothing.
But the rest_<post type>}_query
filter can be used to manually add custom meta queries to the query arguments passed to WP_Query
.
Here's an example for the core post
type, but validation and sanitization is up to you…
add_filter( 'rest_post_query', 'my_filter_rest_post_query', 10, 2 );
function my_filter_rest_post_query( $args, $request ) {
if ( isset( $request['meta_key'] ) ) {
$args['meta_key'] = $request['meta_key'];
// If the request did not include the meta_value parameter, or that it's a
// null, then we do not set the meta_value argument. Which means, the API
// response would include any posts that have the meta, regardless the value.
if ( isset( $request['meta_value'] ) ) {
$args['meta_value'] = $request['meta_value'];
}
}
return $args;
}
So with that, your Backbone code should now work, but a better option, which makes use of the core validation functions, where validation is by default done automatically for you, is by registering your custom request parameter using the rest_<post type>_collection_params
filter.
Here's an example where I used a single custom parameter named meta_status
for your status
meta:
add_filter( 'rest_post_collection_params', 'my_filter_rest_post_collection_params' );
function my_filter_rest_post_collection_params( $query_params ) {
$query_params['meta_status'] = array(
'anyOf' => array(
array( 'type' => 'string' ),
array( 'type' => 'null' ), // Allows a real null value.
),
// Your other arguments here.
);
return $query_params;
}
Now the filter callback might look like:
add_filter( 'rest_post_query', 'my_filter_rest_post_query', 10, 2 );
function my_filter_rest_post_query( $args, $request ) {
if ( array_key_exists( 'meta_status', $request->get_params() ) ) {
$args['meta_key'] = 'status';
// I've allowed a null value, therefore, if the meta_value parameter is a
// null, then we do not set the meta_value argument. Which means, the API
// response would include any posts that have the meta, regardless the value.
if ( isset( $request['meta_status'] ) ) {
$args['meta_value'] = $request['meta_status'];
}
}
return $args;
}
本文标签: How to get posts in backbone where a custom field matches a value
版权声明:本文标题:How to get posts in backbone where a custom field matches a value? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736659848a1946362.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
status
, and then you would be able to easily filter the posts via thetax_relation
parameter in the REST API, without having to use the filter hooks stated in my answer. – Sally CJ Commented Feb 7, 2024 at 19:00