Assuming you mean different images for posts that are in different categories, then sure. If you’re talking about customised featured images for categories then that’s a bit different, because we’re hooking post meta data here, not term meta data.
For posts, all you need to do is find the primary/first category of the post… because a post/product can be in more than one category.
You could create a little function to get the first category id of a post, like this:
function custom_get_first_category(int $post_id) { $category_term = null; $category_terms = get_the_category($post_id); if (is_array($category_terms) && (count($category_terms) > 0)) { $category_term = $category_terms[0]; // <<< This is a WP_Term object } return $category_term; }
Then you'll want to comment out the line that checks to see if the post type has is in HW_MISSING_FEATURED_IMAGE_IDS:
// } elseif (!array_key_exists($post_type, HW_MISSING_FEATURED_IMAGE_IDS)) {
Then you call your new custom_get_first_category() function from the main else{ … } block in custom_default_post_metadata() like this:
} else { if (array_key_exists($post_type, HW_MISSING_FEATURED_IMAGE_IDS)) { $value = HW_MISSING_FEATURED_IMAGE_IDS[$post_type]; } $post_category = custom_get_first_category($object_id); if (!empty($post_category)) { switch ($post_category->slug) { case 'some_category': $value = 123; // Image ID for some_category break; case 'another_category': $value = 456; // Image ID for another_category break; default: // ... break; } } }
I've not tested this (so watch out for typos) but it should be easy enough to get something working from here.