admin管理员组文章数量:1326304
I'm building a mobile app and using the rest API on WordPress to do that, I am customizing the /wp/v2/posts
response by using the following code:
add_filter('rest_prepare_post', 'custome_posts_response', 10, 3);
function custome_posts_response($data, $post, $context)
{
$newspapers = wp_get_post_terms($post->ID, 'newspaper');
$categories = wp_get_post_terms($post->ID, 'category');
$tags = wp_get_post_terms($post->ID, 'post_tag');
return [
"id" => $post->ID,
"title" => $post->post_title,
"format" => $data->data['format'],
"date" => $data->data['date'],
"slug" => $data->data['slug'],
"status" => $data->data['status'],
"externalFeaturedImage" => $data->data['external_featured_image'],
"sourceLink" => $data->data['source_link'],
"content" => $post->post_content,
"excerpt" => $post->post_excerpt,
"author" => $data->data['author'],
"newspaper" => $newspapers,
"categories" => $categories,
"tags" => $tags,
"commentCount" => $post->comment_count,
"commentStatus" => $post->comment_status
];
}
My problem is the term OBJ in my above code print like that:
{
term_id: 4,
name: "Arabs Turbo",
slug: "arabs-turbo",
taxonomy: "newspaper",
description: "",
parent: 0,
count: 181,
filter: "raw"
}
I guess that because it a query for selecting all fields directly from the database and I don't wanna that,
I need the response to be exactly as WP REST API Term model like the following:
{
"id": 9,
"count": 27,
"description": "",
"link": "http://localhost/carstime/newspapers/ahmed-el-wakil/",
"name": "Ahmed El Wakil",
"slug": "ahmed-el-wakil",
"taxonomy": "newspaper",
"meta": [],
"_links": //...
}
So what method/function should I use to accomplish this instead of using wp_get_post_terms
or mapping though the array using array_map
for every taxonomy.
I'm building a mobile app and using the rest API on WordPress to do that, I am customizing the /wp/v2/posts
response by using the following code:
add_filter('rest_prepare_post', 'custome_posts_response', 10, 3);
function custome_posts_response($data, $post, $context)
{
$newspapers = wp_get_post_terms($post->ID, 'newspaper');
$categories = wp_get_post_terms($post->ID, 'category');
$tags = wp_get_post_terms($post->ID, 'post_tag');
return [
"id" => $post->ID,
"title" => $post->post_title,
"format" => $data->data['format'],
"date" => $data->data['date'],
"slug" => $data->data['slug'],
"status" => $data->data['status'],
"externalFeaturedImage" => $data->data['external_featured_image'],
"sourceLink" => $data->data['source_link'],
"content" => $post->post_content,
"excerpt" => $post->post_excerpt,
"author" => $data->data['author'],
"newspaper" => $newspapers,
"categories" => $categories,
"tags" => $tags,
"commentCount" => $post->comment_count,
"commentStatus" => $post->comment_status
];
}
My problem is the term OBJ in my above code print like that:
{
term_id: 4,
name: "Arabs Turbo",
slug: "arabs-turbo",
taxonomy: "newspaper",
description: "",
parent: 0,
count: 181,
filter: "raw"
}
I guess that because it a query for selecting all fields directly from the database and I don't wanna that,
I need the response to be exactly as WP REST API Term model like the following:
{
"id": 9,
"count": 27,
"description": "",
"link": "http://localhost/carstime/newspapers/ahmed-el-wakil/",
"name": "Ahmed El Wakil",
"slug": "ahmed-el-wakil",
"taxonomy": "newspaper",
"meta": [],
"_links": //...
}
So what method/function should I use to accomplish this instead of using wp_get_post_terms
or mapping though the array using array_map
for every taxonomy.
1 Answer
Reset to default 0I got fixed my problem by using the method prepare_item_for_response
in WP_REST_Terms_Controller
function custome_posts_response(WP_REST_Response $data, WP_POST $post, WP_REST_Request $request)
{
$newspapers = wp_get_post_terms($post->ID, 'newspaper');
$categories = wp_get_post_terms($post->ID, 'category');
$tags = wp_get_post_terms($post->ID, 'post_tag');
$newspaper_ctrl = new WP_REST_Terms_Controller('newspaper');
$category_ctrl = new WP_REST_Terms_Controller('category');
$tags_ctrl = new WP_REST_Terms_Controller('post_tag');
$newspapers = array_map(function( WP_TERM $newspaper ) use ($newspaper_ctrl, $request) {
return $newspaper_ctrl->prepare_item_for_response($newspaper, $request)->data;
}, $newspapers);
$categories = array_map(function( WP_TERM $category ) use ($category_ctrl, $request) {
return $category_ctrl->prepare_item_for_response($category, $request)->data;
}, $categories);
$tags = array_map(function( WP_TERM $tag ) use ($tags_ctrl, $request) {
return $tags_ctrl->prepare_item_for_response($tag, $request)->data;
}, $tags);
return [
"id" => $post->ID,
"title" => $post->post_title,
"format" => $data->data['format'],
"date" => $data->data['date'],
"slug" => $data->data['slug'],
"status" => $data->data['status'],
"externalFeaturedImage" => $data->data['external_featured_image'],
"sourceLink" => $data->data['source_link'],
"content" => $post->post_content,
"excerpt" => $post->post_excerpt,
"author" => $data->data['author'],
"newspaper" => $newspapers,
"categories" => $categories,
"tags" => $tags,
"commentCount" => $post->comment_count,
"commentStatus" => $post->comment_status
];
}
本文标签: pluginswpgetpostterms is difference obj model than the one in rest api response
版权声明:本文标题:plugins - wp_get_post_terms is difference obj model than the one in rest api response 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742207219a2433041.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论