Home Forums ACF PRO Avoid using a foreach() with update_field()


Avoid using a foreach() with update_field()

  • Is there a way to update multiples fields from a given post without the use of update_field inside a loop ?

    The only way I’ve managed to update an array of fields is like this :

    $post_id = 1234;
    foreach ($acf_fields as $key => $value) {
      update_field($key, $value, $post_id);

    But it’s a performance nightmare.

  • No there isn’t. ACF uses use update_post_meta() or update_metadata(). Every update requires at least 2 db queries and sometimes 3. This means that it can take 4 to 6 queries to update a single ACF field.

    WP does not supply any way to update more than a single meta value at a time in a single query.

    There is a way, but it would require accessing the database directly and constructing the correct SQL query to do multiple updates or inserts to the correct table in a single query. You would also need to account for both the field value and the acf field reference value as well.

  • Okay, thanks for the clear answer.

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

The topic ‘Avoid using a foreach() with update_field()’ is closed to new replies.