Support

Account

Home Forums Backend Issues (wp-admin) WP Admin updates differently in DB than update_field() ?

Solved

WP Admin updates differently in DB than update_field() ?

  • I’m building a front end form that creates a new post with wp_insert_post() via an AJAX handler. Once I’ve created the post ID, I then use a foreach loop to update_field().

    issue: get_field_object() doesn’t work on the fields I created with update_field(). However, when I update in WP Admin, it works.

    This is my AJAX handler: https://bitbucket.org/snippets/ChristopherArter/n4RRy and I’ve verified in the browser console that the javascript object is correct and that everything on that end is good.

    I’ve got a posts management table that on the front end to display the posts created and it works great.. as long as the post was created or updated from the WP Admin. If not, the get_field_object() doesn’t work, and it’s a mess.

    I took a look in the database, and you’ll see for post 949, the top image is what was saved to the database when I use the update_field() function and the AJAX handler above. The image below is what happens when I go into the WP Admin and hit “Update” on the very same post, which in turn, the get_field_object() works great and everything is good.

    See the database here: http://imgur.com/a/y3moJ

    I’m not really familiar with ACF, so I don’t know what that serialization on the end of the ‘field_’ identifier is..

    Any suggestions on how to get this working as it does when I update from Admin?

  • Hi @chrisarter

    There’s no info on how your $cleanData looks like in your snippet but my guess is that you’re using the field names as key?

    Thing is that if you’re doing update_field for not previously existing values you have to use the field key rather than the field name. Otherwise the matching table value _fieldname with the field key as value aren’t created and because of that you’re going to have trouble using functions like get_field_object().

    The reason it’ll work after you manually hit update is because then ACF itself saves the values for the post with the field key present (so now you’ll have both values in your database).

    Just use the field keys instead and you’ll be good to go! These are hidden by default but can be turned on from the screen options in the top right corner.

  • You’re the man. It worked like a charm! Thank you!

  • Great to hear!
    Best of luck with your form building!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.