- Create a category image using ACF, field = category_image
- This function will automatically (on saving the post or updating) look for the FIRST category assigned to it
- Then it will lookup that categories image and assign as a Featured Image to the Post
/** Sets the featured image on Single Posts based on the assigned Category*/function default_category_featured_image()
{
global$post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists)
{
// Get Category ID
// Get Image attached to the Category - we already have this
// Set Thumbanail
$yourcat = get_the_category($post->ID);
$category_ID = $yourcat[0]->term_id;
$categoryImage = get_field('category_image', 'category_'.$category_ID);
set_post_thumbnail($post->ID, $categoryImage["id"]);
}}
add_action('save_post', 'default_category_featured_image');