Hello,
Thanks for the feedback and the video (you may want to keep it private tho, since you display private part of production website). There’s a lot of plugins and custom code here which could create compatibility issues tho.
I would recommend to clone your website using a plugin like Duplicator and install your website on a local environment to avoid crashing your production.
Regarding your problem, it looks like it is caused by the code you’re using the set the Image Crop field as featured image, from this topic:
add_action('acf/save_post', function ($post_id) {
$value = get_field('my_crop_image_field', $post_id);
if ($value) {
if (!is_numeric($value)) {
$value = $value['ID'];
}
update_post_meta($post_id, '_thumbnail_id', $value);
} else {
delete_post_meta($post_id, '_thumbnail_id');
}
}, 11);
The problem with this code is that everytime a page with ACF fields is saved in the back-end, it will try to to retrieve my_crop_image_field
, and if it is not there it will delete the featured image in else { delete_post_meta($post_id, '_thumbnail_id'); }
.
In your example, the field my_crop_image_field
doesn’t exists on the post type Page
so it will always delete the featured image.
Now the question of “Why disabling ACF Extended seems to fix the problem”:
By default, ACF will trigger the acf/save_post
hook when there at least 1 ACF field on the page. It looks like you have no ACF fields on the Post Type Page
, so without ACF Extended acf/save_post
is not triggered, so my_crop_image_field
is not checked and the featured image is not deleted.
With ACF Extended, the acf/save_post
hook is triggered on your post type Page
(even without any ACF Field) because ACF Extended inject a hidden field for the Ajax Authorbox module (The authorbox which let you change the author from the sidebar). So acf/save_post
is triggered, the field my_crop_image_field
is checked (but doesn’t exists) and the featured image is always deleted.
This why you think disabling ACF Extended fix the issue, because you have no ACF Field on your post type page. Now I’ll show you that this issue is still there even when ACF Extended is disabled:
You just have to create a “Test” field group, with one “Test Field” on the post type Page
. This will trigger acf/save_post
(and your custom code) on the post type Page, and the featured image will be always deleted, because there is no my_crop_image_field
in there.
Here is a video showing this: https://i.imgur.com/ieQosIY.mp4
In this video, ACF Extended is disabled, I added your code in the functions.php
and I created a “Test Field Group” with a simple “Text” field for the Post Type Page
. You can try it yourself.
To fix your issue, you have to enhance the code that check the my_crop_image_field
field. I would recommend to check if $value === false
instead (field is here on the page, but is empty). Code example:
add_action('acf/save_post', function ($post_id) {
$value = get_field('my_crop_image_field', $post_id);
if ($value) {
if (!is_numeric($value)) {
$value = $value['ID'];
}
update_post_meta($post_id, '_thumbnail_id', $value);
// Check if field is here AND empty
} elseif($value === false) {
delete_post_meta($post_id, '_thumbnail_id');
}
}, 11);
Your code now works even if you have an ACF field on the post type Page
and no my_crop_image_field
.
Hope it helps!
Regards.