AdvancedCustomFields / AdvancedCustomFields/acf

Not able to grab acf field dynamically using wp.select in Gutenberg

Open
#134 3 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'm currently using Advanced Custom Fields PRO Version 5.8.0-beta3, ACF to Rest API Version 3.1.0 and the new Gutenberg WP editor and running into some issues grabbing the acf field dynamically in a block.js file. I attempted to use withSelect to grab the data. This has worked for me in the past with data like the title and featured image, but is not dynamically grabbing the acf fields.

edit: withSelect((select, props) => {
    const { attributes, setAttributes } = props;
    const { getEditedPostAttribute } = select('core/editor');
    const advCustomFields = getEditedPostAttribute('acf') || {};
    const excerpt = getEditedPostAttribute('excerpt') ;
   
   console.log(advCustomFields) // if I enter data, outputs nothing until I save two times
   console.log(excerpt) // if I type in the excerpt field in the sidebar,  it dynamically updates and prints the data

    return {
      attributes,
      setAttributes,
      advCustomFields
    }
  })

Expected result:
The fields will dynamically update and I can use that data within my custom gutenberg block.

Actual outcome:

  • The acf field comes out blank. If I save draft once, the acf is still blank. If I save a second time, the block preview renders correctly. However, my team does not feel this is a great editor experience.

Thoughts for why this is happening: The acf data is not connected to the wordpress store and is therefore not updating. Still not sure why saving twice is necessary.

Any ideas how to get around this? Will I need to create a custom action creator? Wondering if anyone else has ran into this issue...?

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 with the block.js edit function and its withSelect callback, then inspect how ACF to REST API exposes acf data to core/editor.getEditedPostAttribute. Reproduce the blank-until-second-save behavior in the Gutenberg editor; done means the ACF values update dynamically without repeated saves.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, php, wordpress
Domain
content, frontend
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.