1

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 1

0

I 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
    ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.