• Hi,

    This code automatically adds a featured image to all posts, how can I exclude by ID some posts from this code? I have already researched enough and I could not achieve it, I would appreciate your help

    Thank you

    // Auto add featured image
    function wpsites_auto_set_featured_image() {
       global $post;
       $featured_image_exists = has_post_thumbnail($post->ID);
          if (!$featured_image_exists)  {
             $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);}
             }
          }
    }
    add_action('the_post', 'wpsites_auto_set_featured_image');
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter devav

    (@devav)

    I’m certainly trying to automatically add the featured images to all the posts, but this code adds them to the pages too and that’s what I’m trying to remove.

    Moderator bcworkz

    (@bcworkz)

    Try expanding the first conditional like so:
    if (!$featured_image_exists && 'post'== $post->post_type) {

    The featured image is only assigned to blog posts which still need it, not pages or any other post type. If you still really wanted to exclude by ID, you’d do something like && ! in_array( $post->ID, [123,234,345,])

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Exclude specific post from this code’ is closed to new replies.