0

I have a site using the WordPress REST API and advanced custom fields ACF. I use the plugin ACF TO REST API to expose the AC fields to the REST API. So far so good.

But now I want to add normal functionality to the «Preview Changes» button within the wordpress backend. I managed to change the buttons functionality and I also learned that pushing it does autosave the post into a new revision. To access and display the preview I must get the data of the newest revision of the post. I can do that but the REST API does only expose core wordpress stuff (like title, content, excerpt etc.) but not the ACFs belonging to that post.

I would like to access a wordpress REST API route to get the newest revision of the post including the content of the (changed) ACF fields.

Could somebody help me out how to write a custom route which does exactly that? I tried to add this functionality to the ACF TO REST API plugin, but so far I had no success.

I think I have call a REST API route where I include the posts (or pages) id, then it makes a call returning me the revisions of that page (which i already get here > wp/v2/pages/51/revisions/,

This shows me an array of revision-entries like this:

{
    "author": 1,
    "date": "2017-11-17T18:19:51",
    "date_gmt": "2017-11-17T17:19:51",
    "id": 461,
    "modified": "2017-11-17T18:19:51",
    "modified_gmt": "2017-11-17T17:19:51",
    "parent": 51,
    "slug": "51-autosave-v1",
    "guid":
    {
        "rendered": "http://[DOMAIN]/51-autosave-v1/"
    },
    "title":
    {
        "rendered": "post title"
    },
    "content":
    {
        "rendered": "post content"
    },
    "excerpt":
    {
        "rendered": ""
    },
    "_links":
    {
        "parent": [
        {
            "href": "http://[DOMAIN]/api/wp/v2/pages/51"
        }]
    }
},

but now I only want the newest revision. The newest revision seems to be like a normal post and has a normal post ID (I call it RevisionID, which would be 461 in the example above).

Then I need to query all the post_meta entries with 'post_id' = RevisionID and merge the results with the results from the newest revision.

Can someone confirm that?

I also would be very glad with some help how I can set up the queries for the Wordpress REST API to filter the results I want.

thanks for any help here.

Cheers

Merc
  • 4,241
  • 8
  • 52
  • 81
  • so far I wrote my own WP_REST_Controller (extends that one). I pretty much manually get the latest revision (-id) of a post (id passed as parameter in uri) > `wp_get_post_revisions`. Then I use `get_post_meta($id_of_revision)` to get the custom fields associated with this revision (which is my preview version). This kind of gives me the right results but they are very messy. Also nested information like the one of an ACF with `type = image` is not available. The plugin ACF TO REST API does somehow manages to get these fields into a neat json, which I don't. Maybe someone could help me out.✌ – Merc Nov 19 '17 at 22:38

1 Answers1

2

At this moment my plugin ACF to REST API does not supports revisions, so you need to use that snippet below.

I'll plan to release this feature in the next month.

add_filter( 'rest_prepare_revision', function( $response, $post ) {
    $data = $response->get_data();

    $data['acf'] = get_fields( $post->ID );

    return rest_ensure_response( $data );
}, 10, 2 );

Endpoints:

http://localhost/wp-json/wp/v2/posts/{post_id}/revisions

http://localhost/wp-json/wp/v2/posts/{post_id}/revisions/{revision_id}

References:

https://github.com/airesvsg/acf-to-rest-api/issues/190#issuecomment-345854148

https://developer.wordpress.org/rest-api/reference/post-revisions/

Thanks