It can be very easy to forget to set the featured image. Here is code that will do the following:
- Sets the featured image.
- If no featured image then look for category image.
- If no category image set the first post image.
- If no post image it will set a fallback image.
I did a lot of searching online and came across an article from
https://wpforce.com/automatically-set-the-featured-image-in-wordpress/ and Add Default Featured Image For Each Post In A Category
Wpforce had most of the code. I took the category code from wpsites. I then merged the category code into the featured images code.
The attachment id
Since the code uses post-id/attachment id one needs to find the image data id. Go to the media library and right click the image you want to find the data id to. Select Inspect Element then look into the html code and you should notice the data-id number of the image. This is the number you need to use.
Add the following code into your functions file.
// Inside your functions file add the following code
//
function wpforce_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
if (!$already_has_thumb) {
// If post does not have a featured image then get the first post image and set as featured image.
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); // Number 1 relates to taking post image number 1 and adding it as a featured image.
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
//$attachment_id = attachment_url_to_postid( $image_url );
// echo $attachment_id;
}
} else if ( in_category('WordPress') ){ // Add your own categories.
set_post_thumbnail($post->ID, '21');
// Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id.
}
else if ( in_category('test') ) {
set_post_thumbnail($post->ID, '30');
}
else if ( in_category('images') ) {
set_post_thumbnail($post->ID, '111');
}
else if ( in_category('Uncategorized') ) {
set_post_thumbnail($post->ID, '111');
}
}
} //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');
If you do not want to add the else if categories then adjust the first else if to:
} else {
set_post_thumbnail($post->ID, '21'); // Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id.
}
Then remove each else if that follows.
I also have a code specific for Genesis themes. (It seems to work better then the general featured image code above. I am looking into taking the Genesis featured images code and making it work for most themes in addition to Genesis themes.) This code looks for images through the url.
// GENESIS get featured image.
// 1. Sets the featured image.
// 2. If no featured image then get image from category.
// 3. If no category image then get the first post image.
// 4. If no post image or category image then set a fallback image.
// Add to your functions file.
// Resources
// https://wordpress.org/support/topic/make-first-image-in-post-featured-if-no-featured-is-set?replies=9
// http://wpsites.net/web-design/add-default-featured-image-for-each-post-in-a-category/
// https://codex.wordpress.org/Conditional_Tags
function get_src() {
if ( has_post_thumbnail() ) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumb' );
$fbimage = $src[0];
} else {
global $post, $posts;
$fbimage = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
$post->post_content, $matches);
$fbimage = $matches [1] [0];
}
if(empty($fbimage) && in_category('WordPress') ) {
$fbimage = get_stylesheet_directory_uri().'/images/Leaves-white.jpg';
}
if(empty($fbimage) && in_category('images') ) {
$fbimage = get_stylesheet_directory_uri().'/images/Tree-road-sun.jpg';
}
if(empty($fbimage) && in_category('Uncategorized') ) {
$fbimage = get_stylesheet_directory_uri().'/images/front-page-1.jpg';
}
if(empty($fbimage)) {
$fbimage = get_stylesheet_directory_uri().'/images/Red-back.jpg';
/*$fbimage = site_url().'/wp-content/uploads/Red-back.jpg'; Works.
$fbimage = site_url().'/images/Red-back.jpg'; Works
$fbimage = CHILD_URL.'/images/Red-back.jpg'; Works - Genesis. But do not use CHILD_URL.*/
}
return $fbimage;
}
add_filter('genesis_get_image', 'default_image_fallback', 10, 2);
function default_image_fallback($output, $args) {
return get_image();
}
function get_image($class="") {
$src = get_src();
ob_start()?>
<a href="<?php echo get_permalink() ?>">
<img class="featured-image <?php echo $class ?>" src="<?php echo $src ?>" alt="<?php echo get_the_title() ?>" />
</a>
<?php return ob_get_clean();
}
Removing the featured images
If you need to remove your featured images (I had to when I was testing the above featured images code to clear the cache in WordPress.)
add_action ('init', 'remove_featured_images' );
function remove_featured_images () {
global $wpdb;
// The following query will only unset the featured image, it will not delete it.
$wpdb->query( "
DELETE FROM $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
" );
}
The code is from: https://spicemailer.com/wordpress/how-to-remove-featured-image-from-all-posts-wordpress/ There is also variations of the above code located at the spicemailer web site.