Support

Account

Home Forums Backend Issues (wp-admin) update_field if field is empty else continue

Solving

update_field if field is empty else continue

  • Hello, I have a custom taxonomy and add entries to this taxonomy programmatically with data I get from an API. The custom taxonomy has some acf fields I also fill in this process.

    This API doesn’t have data on some fields in some entries. So with data from another API I want to iterate over the taxonomy again and only fill the empty fields.

    When I just do the same import process with the second API, update_field will update the empty fields but also change the data of the fields that already have data from the first API.

    So, I want to check If a field is empty before update_field. Is there a function which checks if a field is empty? I couldn’t find a solution yet. I tried with

    if(get_field($term_id) === '') {
      update_field($key, $value, $term_id);
    }

    But it is not working…
    Does anyone have a suggestion?

    BR

  • Maybe I have to also state, that I am trying to do this inside a loop (foreach)

  • Must of the time the field vale will be false or NULL if it is empty. You need to check for empty

    
    if(empty(get_field($term_id)) {
    

    this might also work

    
    if (!get_field($term_id)) {
    

    However, this could be a problem if the actual value of the field is something that evaluates to empty but is a valid value, for example an empty string, 0, false, etc. So depending on the type of field you might need to check for different things.

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

You must be logged in to reply to this topic.