AdvancedCustomFields / AdvancedCustomFields/acf
ACF PRO 5.9.2: Draft Preview not showing Custom Fields
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 945
- Forks
- 197
- PR merge metrics
- No merged PRs in 30d
Description
I think I've found yet another issue with Wordpress' Post Preview Feature.
I realise that problems with this feature came and went, and if I've create a duplicate here, I'm sorry.
Description
I have a template single.php that uses get_field('myFieldName', $post->ID) to retrieve a custom field.
I assume this is a very common scenario.
When I save a post as draft, the returned value is empty.
When I publish the post, everything works as expected and text is returned.
Rabbit Hole
I noticed get_field() makes use of acf_get_valid_post_id() in /advanced-custom-fields-pro/includes/api/api-template.php, l.24.
This function is passed the $post_id which is the ID - 273348 - of the post shown in the backend (as in /post.php?post=273348&action=edit) as well es in the frontend (as in /?p=273348&preview=true).
This function returns - however - a different ID - 273350 - which is the ID of the latest revision of that post, I found looking in the database. Somehow, it seems that field values cannot be retrieved from the revision, but only from the post itself.
I'm not sure this even is or should be the desired behaviour. And if it is, I'm not sure why the revision would not contain or not support getting the values of my field.
Workaround
Going further into the rabbit hole I saw that there is a filter in /advanced-custom-fields-pro/includes/api/api-helpers.php, l. 2673, which I could use to make acf_get_valid_post_id() return the original ID.
However, this seems very invasive and I'm hesitant to apply such a fix.
Using Wordpress 5.5.3 and ACF Pro 5.9.2 - although I assume the same issue exists in the non-pro version.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in advanced-custom-fields-pro/includes/api/api-template.php and trace get_field() through acf_get_valid_post_id(), then inspect the related filter in advanced-custom-fields-pro/includes/api/api-helpers.php around line 2673. Reproduce draft preview retrieval with WordPress 5.5.3 and ACF Pro 5.9.2, comparing post ID 273348 with revision ID 273350. Done means custom fields are retrieved correctly in draft previews without relying on the reported workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, wordpress
- Domain
- backend, content
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100