Support

Account

Home Forums ACF PRO update_field() returning FALSE if value to update is not changed…

Solving

update_field() returning FALSE if value to update is not changed…

  • HI all,

    I want to share what I found (new for me!). I am getting crazy to understand why a simple

    update_field(‘myfieldjey’, ‘xyz’, $myPostID);

    is returning FALSE ( = error), but in some cases returning TRUE.

    Simple (?): if the value in DB is not ‘xyz’ the function will return TRUE. FALSE will be returned in case of error OR in case the value in the DB is already ‘xyz’…

    Now I discover that WP update_metadata() function will return FALSE if the value to update is not changed.. (see https://core.trac.wordpress.org/ticket/21900).

    in other words….in some cases if update_field() is returning false it does not means error…!

    ELLIOT: what about mananing the return value of update_field() correctly? Even WP is not doing this…

    To me, like it is now, this is generating a big confusion…

  • I’m not sure exactly what the problem is. update_field() returns the same thing that update_post_meta() returns. Either true the field was updated or false the value was not updated, and ACF does not alter what is returned by update_metadata() so this is working the same way that WordPress core works. If you notice the ticket you linked to is closed and not going to be fixed.

    Basically there can’t be an error, either the value was updated or it wasn’t.

    I’ve dug through the WP code and the only time anything other than true or false is returned is if a add_post_meta() is called. In this case the meta_id is returned and that is what ACF will return.

    I’ll mark this for the developer to look at. Please give more detail on how this is not functioning the same way that WP core functions.

  • Hi the problem is that the documentation says that update_field() return TRUE if all ok, FALSE in case of failure.

    Instead is also returning FALSE if the value is not updated (because the value you pass to the function is the same with the current DB value).

    It should be CLEAR that the function return FALSE not only in case of failure.
    (and I agree that the problem is also at WP level).

    This because if you write a code like this:

    if(!update_field(…)) handle_error(…);

    you get crazy (because in some case there is no error to handle !)

  • Maybe I’m looking at a different page, I don’t see anything mentioning what’s returned here http://www.advancedcustomfields.com/resources/update_field/

  • it is not written, I agree, but to me clarifying how it works will help a lot.
    As normally getting a FALSE from an API is managed as an error.

    My 2 cents.
    🙂

  • I can understand your frustrations a bit, but I can also guess to some degree the developer’s thinking is that it works consistent with the way update_post_meta() works and update_field() is just a wrapper for update_post_meta().

    Returns meta_id if the meta doesn’t exist, otherwise returns true on success and false on failure. NOTE: If the meta_value passed to this function is the same as the value that is already in the database, this function returns false.

    Maybe when he gets time he’ll add it to the documentation. I’m also assuming that since update_field() does not mention returned values that it’s not expected that anyone would be testing the returned value. The function is not going to “Fail”, after it is called the field value for the post_id will be set to you want to update it to, one way or another.

  • Why not put this in the documentation? The documentation does not even mention the potential return values.

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

The topic ‘update_field() returning FALSE if value to update is not changed…’ is closed to new replies.