Removing Image Sizes for Custom Post Type

The question:

I’m trying to go about this without the use of a plugin, but actions and filters

Is there a way to remove extra image sizes generated by WordPress based on the Custom Post Type? I’ve been trying to work with the intermediate_image_sizes_advanced filter but it doesn’t look like it has access to $post or post_type. So I’m using the function like so:

function filter_image_sizes($sizes) {
    global $post;
    global $post_type;

    if($post->post_type == 'cpt_slides' || $post_type == 'cpt_slides'){
        unset( $sizes['thumbnail']);
        unset( $sizes['large']);

    return $sizes;
add_filter('intermediate_image_sizes_advanced', 'filter_image_sizes');

The Reasoning / Situation

I’ve created a Slides Custom Post Type (like an Image Slider) that uses the Featured Image to allow the user to upload “Slides”. I also have a few custom image sizes, but one specifically for the slides alone. Since the slide images are going to be one static size, I don’t want WordPress to generate all these extra image sizes I’ll never use, hence the question.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

May be this filter should work intermediate_image_sizes

Note: This solution will work if you are uploading an image from post edit screen. (tested on localhost with WP-3.8.1)

add_filter( 'intermediate_image_sizes', 'ravs_slider_image_sizes', 999 );
function ravs_slider_image_sizes( $image_sizes ){

    // size for slider
    $slider_image_sizes = array( 'your_image_size_1', 'your_image_size_2' ); 

    // for ex: $slider_image_sizes = array( 'thumbnail', 'medium' );

    // instead of unset sizes, return your custom size for slider image
    if( isset($_REQUEST['post_id']) && 'your_custom_post_type' === get_post_type( $_REQUEST['post_id'] ) )
        return $slider_image_sizes;

    return $image_sizes;

Method 2

Part of the problem may be that this filter could end up being called from more than one place, or perhaps a place where no post is associated with the query. In fact the filter being used appears to only be used when the attachment is first created as a part of the metadata.

Doing a bit of testing shows me that when I’m uploading an image the $post and $post_type variables are not set at the location that this filter is used. Likely this happens because the media upload is done as a background process via javascript instead of push a form submit and reloading the page.

WordPress isn’t really setup at all to handle this kind of filtering, it’s perhaps something that should be considered for future updates, I would suggest submitting a ticket to the WordPress Trac and getting some advice there.

Basically WordPress is storing your media elements independent of the post they may be attached to so that you can use the same file easily in more than one place. If WordPress were to allow you to adjust the sizes generated on a more granular basis, you could end up with situations where you uploaded an image for a blog post and then wanted to reuse that image somewhere else as a featured image. The media library is setup to handle this sort of stuff great, but it isn’t setup to regenerate thumbnails on a case by case basis, it’s setup to generate all the required thumbnails because you may use any or all of the thumbnails elsewhere.

The easiest way to handle this would be to use your own media upload function for the custom post type’s metabox when you’re uploading media. This would allow you to bypass the normal WordPress thumbnail process and only create the sizes that you desire when the file is uploaded. Here’s a great tutorial that will help you with that process:

Method 3

Maybe you could install this plugin:

This plugin can add image sizes and override the theme’s custom sizes. Then you can regenerate them.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment