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.