AdvancedCustomFields / AdvancedCustomFields/acf

update_field() not working within acf/update_value filter

Open
#440 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

I've been trying a couple of different things here. Whenever a field prepended with option_ is saved, I would like to not only save it to the post/block where it is being updated, but also save it as a global "acf option". I have a demo set up with a registered gutenberg block with the field option_demo. The following code doesn't seem to do the trick. I even tried to update_field for the first post in my WP install, and it wouldn't save there either:

add_filter( 'acf/update_value', function( $value, $post_id, $field, $original ) {

  if( substr( $field['name'], 0, 7 ) !== 'option_' ) {
    return $value;
  }

  // This is to avoid infinite loops....
  if( stripos( $post_id, 'option' ) !== false || $post_id == 1 ) {
    return $value;
  }
  
  // Neither one of these work
  update_field( $field['key'], $value, 'option' );
  update_field( $field['key'], $value, 1  );

  return $value;

}, 10, 4 );

Could the problem be related to the fact that the update initiating this whole is within a gutenburg block?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the registered Gutenberg block case with the supplied acf/update_value callback, then trace the update_field calls using the option_ field key and post IDs 'option' and 1. Done means the field value is saved both to the updated block or post and to the global ACF option without causing recursive updates.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.