WP-API / node-wpapi

An isomorphic JavaScript client for the WordPress REST API
http://wp-api.org/node-wpapi/
MIT License
1.68k stars 190 forks source link

can't post data to ACF fields on attachments #422

Open peter-mumford opened 5 years ago

peter-mumford commented 5 years ago

This is very strange. I can create and update posts and pages. I can set ACF field data on posts and pages. But I can't set data on ACF fields on attachments. Create and update will both post data to attachments, but not the ACF fields.

I'm using this straightforward code:

var apiRootJSON = require('../assets/root-dev-accreditation.json');

var site = new WPAPI({
    endpoint: 'https://dev.intranet.example.com/accreditation/wp-json',
    routes: apiRootJSON.routes,
    username: "peter.mumford",
    password: "XXXXXXX"
});

site.media().id(129).update({
    title: 'Peter Mumford',    // THIS DOES SAVE 
    fields: {    // FIELDS DO NOT SAVE
        requested_by: 'peter mumford',
        fmc_doc_number: '778d8df7f7',
    }
}).then(function(res){
    console.log('we updated this media: ' + res.id);
});

In this example, the title updates correctly, and the response ID is logged to the console. But the fields do not update.

I can also update custom fields on Posts and Pages without any problem.

peter-mumford commented 5 years ago

Possibly this issue is a problem in the ACF to REST API plugin. I posted an issue there too: #292.

Maybe it has something to do with how attachments save data—there is no save button. Attachments seem to auto-save.

peter-mumford commented 5 years ago

This is solved by a setting in the custom field group. Solution was here: acf-to-rest-api-example

peter-mumford commented 4 years ago

The fix (setting an additional parameter in the field group location settings to current user--is equal to--logged in) no longer works. This always did seem like an unlikely work around. Possibly recent security updates have invalidated it.

photocurio commented 4 years ago

I found a solution to this. I de-activated the ACF to REST API plugin, and added my custom fields to the API this way: $string_args = array( // if the field is boolean, use 'type' => 'boolean' 'type' => 'string', 'description' => 'A meta key description.', 'single' => true, 'show_in_rest' => true ); register_post_meta( 'attachment', 'doc_name', $string_args );

Then I post like this: wp.media().id(mediaId).update({ title: mediaTitle, meta: { doc_name: docName } }).then(function( response ) { console.log( response ); })