I want to do following...
1 - Publish the new post, 2 - Check if this new post has a featured image, 3 - If it has not a featured image, define its category and save the specific prepared image as the featured image. = Preparing different images for each categories.
Please have a look at the following code that I am using in my functions.php. but it is not working for me.
add_action('save_post', 'wp_force_featured_image', 20, 2);
function wp_force_featured_image($post_id, $post) {
if( $post->post_type == 'post' && $post->post_status == 'publish' ) {
if(!isset($_POST['_thumbnail_id'])) {
$post_categories = get_the_category( $post->ID );
$cats = $post_categories->term_id;
if ( $cats = 1 ) {
add_post_meta( $post_id, '_thumbnail_id', '3135' );
}
elseif ( $cats = 2 ) {
add_post_meta( $post_id, '_thumbnail_id', '3138' );
}
}
}
}