Title: Exclude specific post from this code
Last modified: December 19, 2020

---

# Exclude specific post from this code

 *  [devav](https://wordpress.org/support/users/devav/)
 * (@devav)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/exclude-specific-post-from-this-code/)
 * 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](https://wordpress.org/support/users/devav/)
 * (@devav)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/exclude-specific-post-from-this-code/#post-13810590)
 * 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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/exclude-specific-post-from-this-code/#post-13812394)
 * 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.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/exclude-specific-post-from-this-code/#post-13812394)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
