Home › Forums › General Issues › Setting the post Featured Image based on (PDF) File upload field thumbnail
I’m trying to set the post Featured Image to be the thumbnail of a PDF that is uploaded to a file field (if the post has the category ‘publication’).
The following code I wrote works when I first upload a file to the field, but once I remove that file and replace it with another, I get a 503 error when I save the post again/when I load Posts in the backend/load any page that loads that post. I then have to use wp_trash_post to delete that post to stop it wreaking havoc on my WordPress installation.
function set_publication_post_thumbnail($post_id) {
if(has_category('publication')) {
$publication = get_field('publication', $post_id);
$attachment_id = $publication['ID'];
update_post_meta($post_id, '_thumbnail_id', $attachment_id);
}
}
add_action('save_post', 'set_publication_post_thumbnail');
Appreciate your help with this
This code is actually working – it’s some other code I have written that causes the 503 error!
Hi Damiane,
I do need exactly the same, great help. But is your code still working with WP 5.9.2?
I tried it, but nothing happens.
Best regards
Uli
Hey mate,
Yes, the code is still working with 5.9.2.
A couple things that you need to make sure of –
Let me know if you still can’t get it working after checking the above.
Hi Damiane,
i already did exchange every “publication” with “downloadcenter”, wich is the name of my category, but had file URL as type. Now i changed it to “File Array”, but still nothing happens:
I open a post with category “downloadcenter”, delete the attached file and assign a new file (PDF) and publish it. Now there should be a featured image, I guess – but there isn’t.
Here’s my code:
function set_downloadcenter_post_thumbnail($post_id) {
if(has_category('downloadcenter')) {
$downloadcenter = get_field('downloadcenter', $post_id);
$attachment_id = $downloadcenter['ID'];
update_post_meta($post_id, '_thumbnail_id', $downloadcenter_id);
}
}
add_action('save_post', 'set_downloadcenter_post_thumbnail');
Hi Damiane, I can’t see your reply as it has been marked as private.
Whoops sorry @ulinn
Can you check your Site Health on the WordPress dashboard and check that you have all the PHP extensions that WordPress needs? I’ve previously had an issue where WordPress could not create a thumbnail for a PDF because there was a missing extension.
Otherwise I’d be happy to take a quick look at the site if you want me to.
Site health is good, PHP version is 7.4.5
If I post credentials and mark them as “private”, only you will see them, correct?
Unfortunately I don’t think that is how the Private visibility works – that’s what I tried to do with my earlier reply. So don’t put your credentials on here!
The ACF field Label is “Download”, and the field Name is “downloadcenter”.
The category name is “Downloadcenter” and the category slug is “downloadcenter”.
An FYI RE Private Comments. These can be seen only by the thread starter and someone that has admin access on the forum. A long time ago the original author of ACF used these forums for support. This is no longer the case but the feature still lingers.
Hey @ulinn
Sorry I just took another look at your code and found the issue – in update_post_meta, you are setting the value of the _thumbnail_id meta to the variable $downloadcenter_id which has not been defined.
Update that line to the following –
update_post_meta($post_id, '_thumbnail_id', $attachment_id);
Test and let me know if you still have any issues with it!
Hi @damianemuller,
thank you for looking over my code. I could have realized this myself, sorry.
But: Also with the updated line nothing happens. I did change the attachment of a post, updated the post – but there still is no featured image.
I found part of the problem – my child theme was not active, so the code did not work. Sorry for that …
After activating there is still no thumbnail, but now there is a “replace featured image” button in my post (see screenshot), so something happend to the post. But it does not to seem a vaild image file.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.