• I found some code online and made some edits to fit my needs, but I can’t seem to get it to work. It is a function that automatically sets the featured image for a post that doesn’t currently have on. Here is what it should do:

    If no featured image is found, grab the first image attachment in the post and use that.
    If there is no image in the post, it should then check to see if the post is in 1 of 5 categories. If so, I have created default images and set by ID to display.
    If it is not in those categories and still doesn’t have an image, there is one more fallback image that will be used.

    The one unique thing is that I’m recieving these post from the front end of the site, not the admin end.

    See the site here: https://www.theseattlevine.com

    Here is the code:

    // Auto-find the featured image for a post with an image
    function autoset_featured() {
              global $post;
              $already_has_thumb = has_post_thumbnail($post->ID);
                  if (!$already_has_thumb)  {
                  $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                              if ($attached_image) {
                                    foreach ($attached_image as $attachment_id => $attachment) {
                                    set_post_thumbnail($post->ID, $attachment_id);
                                    }
                               } else {
    
    									if ( in_category('user-audio') ) {
    										foreach ($attached_image as $attachment_id => $attachment) {
    										set_post_thumbnail($post->ID, '3891');
    										}
    									} elseif ( in_category('user-video') ) {
    										foreach ($attached_image as $attachment_id => $attachment) {
    										set_post_thumbnail($post->ID, '3894');
    										}
    									} elseif ( in_category('user-image') ) {
    										foreach ($attached_image as $attachment_id => $attachment) {
    										set_post_thumbnail($post->ID, '3892');
    										}
    									} elseif ( in_category('user-writing') ) {
    										foreach ($attached_image as $attachment_id => $attachment) {
    										set_post_thumbnail($post->ID, '3895');
    										}
    									} elseif ( in_category('user-link') ) {
    										foreach ($attached_image as $attachment_id => $attachment) {
    										set_post_thumbnail($post->ID, '3893');
    										}
    									} else {
    										foreach ($attached_image as $attachment_id => $attachment) {
    										set_post_thumbnail($post->ID, '3896');
    										}
    									}
    
                               }
                            }
          }  //end function
    add_action('new', 'autoset_featured');
    add_action('the_post', 'autoset_featured');
    add_action('save_post', 'autoset_featured');
    add_action('draft_to_publish', 'autoset_featured');
    add_action('new_to_publish', 'autoset_featured');
    add_action('pending_to_publish', 'autoset_featured');
    add_action('future_to_publish', 'autoset_featured');

    I have a feeling I may not be using in_category correctly. Or if anyone else can see any other problems, let me know how you think I can fix them.

    Thanks!

  • The topic ‘Auto-set the featured image’ is closed to new replies.