Support

Account

Home Forums Backend Issues (wp-admin) Saving Default Value to Custom Fields

Solving

Saving Default Value to Custom Fields

  • I have a few Custom Fields with Default Values as “N/A”.
    I worked with them and generated around 2000 Posts.

    Yesterday I added a few more Custom fields to the group with the same default value as N/A.

    Now when Access the post data through a JSON API these are the scenarios I face

    • The Posts Being Published after adding the new custom fields show the data for old custom fields in the JSON
    • The Posts already Published before adding the new custom fields DO NOT show the Custom field and corresponding data for all the new custom fields in the JSON
      http://romindex.com/wp-json/acf/v2/post/12752
    • If I open post editor for any old post, and click update button with no changes made. The NEW and OLD custom fields are again visible.
      http://romindex.com/wp-json/acf/v2/post/12738

    It is not practically possible to update 2000 posts, I tried bulk update from all post page. But that actually doesn’t work.

    Can you help me?
    As I am building an android app, and it crashes when getting data from something that doesn’t exist in the JSON API.

  • What plugin are you using for the JSON API that you mentioned.

    There isn’t any real way to do what you want in ACF unless the API you’re using calls ACF functions. What you need to do is filter the values returned by the JSON API and the best answers for how to do that will come from the documentation and support for that API.

    If you want to update all the existing posts to add new fields and remove old fields then that’s going to be a manual process.

    You will need to 1) Get all the posts that you want to change. 2) Use delete_post_meta() to delete all the ACF content that you no longer want and 3) Use update_post_meta() to add all the new content.

    In steps 2 & 3 don’t forget that you will also need to delete and add the acf field key references.

  • That’s not a problem with any plugin but with acf itself. If you add new fields with default values to existing pages acf will not return these default values. Default values are only for set them if you are updating a page in the wordpress admin manually. That is the most annoying problem with acf for me 🙁

  • The way ACF works, it cannot associate a field with a post when getting a field value. get_field() is just a wrapper for get_post_meta() and it is the same for all acf functions. ACF does not know that a field group is related to a certain location except when editing that location.

    The location rules are complicated and what your expecting is that ACF will check the location rules for every field group every time you use get_field(). That would be simply impossible, it would time out the load of every page.

    ACF is also not going to retroactively find every post, term, or options page that might be associated with a field group when you save the field group. Again, this would be impossible and will likely time out update process.

    There are some things in WP that it is not possible to do using the functions that WP supplies. The only way to do these things would be if the developer of ACF build a custom interface to deal with the database and to manage the things that WP is already managing.

    As has been said many times in many other posts on this forum. ACF uses existing WP functions. If you did

    
    get_post_meta($post_id, $field_name, true)
    

    and there is nothing stored in that field then it will return nothing. This is all that ACF is doing. If you want there to be a default value then you need to code that default value in.

    
    $value = get_post_meta($post_id, $meta_key, false);
    if (!$value) {
      $value = 'default value';
    }
    

    Hope that helps explain it.

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

The topic ‘Saving Default Value to Custom Fields’ is closed to new replies.