0
votes

Is there a way to return the post title of a custom post type with the ACF to REST-API plugin? When I run my query on the post type all I get is the post id, and then any fields I've set within, but I'd like to not have to create another field in ACF called "title" if I can grab it similar to how I would were I just using ACF without calling through the REST-API.

Here is my GET request for a custom post type called "books":

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

That post type has fields for the author name, and a relationship field for associating book passages to books. Here is what that returns (just showing one book here):

[
    {
        "id": 15,
        "acf": {
            "author": [
                {
                    "first_name": "Graham",
                    "last_name": "Harman"
                }
            ],
            "book_passage_relationship": ""
        }
    }
]
1

1 Answers

0
votes

So this is not tested but I poked around the source code of the ACF to REST-API and found this filter:

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

I think you can use it something like below (may need some tweaking):

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;
});