WP_GET_POST_TERMS对象模型与REST API响应中的对象模型不同

时间:2020-08-06 作者:hesham shawky

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 个回复
SO网友:hesham shawky

我用这个方法解决了我的问题prepare_item_for_response 在里面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
    ];
}

相关推荐

在PHP中删除ECHO输出中的重复项

现在,下面的函数输出产品的所有变体图像。E、 g.3倍蓝色(S、M、L号),3倍红色(S、M、L号)我想下面的功能只输出唯一的彩色图像。E、 g.1个蓝色,1个红色尝试在回显字符串中使用array\\u unique,但无法使其正常工作。谢谢你的帮助。function loop_display_variation_attribute_and_thumbnail() { global $product; if( $product->is_type(\'variable\