使用 ACF 将帖子标题返回到 REST-API

问题描述 投票:0回答:1

有没有办法使用 ACF to REST-API 插件返回自定义帖子类型的帖子标题?当我对帖子类型运行查询时,我得到的只是帖子 ID,然后是我在其中设置的任何字段,但如果我可以抓住它,我不想在 ACF 中创建另一个名为“标题”的字段类似于我只使用 ACF 而不通过 REST-API 调用。

这是我对名为“书籍”的自定义帖子类型的 GET 请求:

http://localhost:8888/wp-json/acf/v3/books

该帖子类型具有作者姓名字段和用于将书籍段落与书籍相关联的关系字段。这是返回的内容(此处仅显示一本书):

[
    {
        "id": 15,
        "acf": {
            "author": [
                {
                    "first_name": "Graham",
                    "last_name": "Harman"
                }
            ],
            "book_passage_relationship": ""
        }
    }
]
php wordpress advanced-custom-fields wordpress-rest-api
1个回答
0
投票

所以这还没有经过测试,但我浏览了 ACF 到 REST-API 的源代码并发现了这个过滤器:

'acf/rest_api/' . $this->type . '/get_items'

我认为你可以像下面这样使用它(可能需要一些调整):

add_filter('acf/rest_api/books/get_items', function($response, $request) {

  // map through response adding the title foreach item
  $response = array_map(function($book) {
    return array_merge($book, ['title' => get_the_title($book['id'])]);
  }, $response);


  // make sure to return the response after we alter it
  return $response;
});
© www.soinside.com 2019 - 2024. All rights reserved.