Remove All Image Sizes By Category or Custom Post Type

This first example removes all image sizes for everything except regular posts. So, additional image sizes will not be created for Pages, custom post types, or in the media library.

// Remove all image sizes EXCEPT for regular posts

add_filter('intermediate_image_sizes', function($sizes) {
    $rem = 1;
    if ('post' == get_post_type($_REQUEST['post_id'])) $rem = 0;
    return $rem ? array() : $sizes;
}, 99);

Alternate Examples

Remove For All But 1 Category

The following removes all image sizes for all posts except posts of a specific category. You specify your category ID on line 9 by changing 123 to your own category ID. If you prefer to use a category slug instead of ID number, see the next code snippet.

// Remove all image sizes EXCEPT for a specific category ID

add_filter('intermediate_image_sizes', function($sizes) {
    $rem = 1;
    $type = get_post_type($_REQUEST['post_id']);
    
    // @todo replace 123 with category ID that should KEEP CREATING images sizes

    if ('post' == $type && in_array( 123, wp_list_pluck( get_the_category($_REQUEST['post_id'] ), 'term_id' ) )) $rem = 0;
 
    return $rem ? array() : $sizes;
}, 99);

The following does the same thing as the previous code snippet, but it lets you give your category slug instead of the category ID number. So, it removes all image sizes except for posts of your specified category. You specify your category ID on line 9 by changing CATEGORY_SLUG to your own category slug.

// Remove all image sizes EXCEPT for a specific category SLUG

add_filter('intermediate_image_sizes', function($sizes) {
    $rem = 1;
    $type = get_post_type($_REQUEST['post_id']);
    
    // @todo replace 123 with category ID that should KEEP CREATING images sizes
    
    if ('post' == $type && in_array( 'CATEGORY_SLUG', wp_list_pluck(get_the_category($_REQUEST['post_id']), 'slug') )) $rem = 0;
 
    return $rem ? array() : $sizes;
}, 99);

Remove For All But Specific Custom Post Types

The following removes all image sizes for everything except specific custom post types. You list your own custom post types on line 5 by replacing CUSTOM_TYPE_1 and CUSTOM_TYPE_2 with a comma-separated list of your own custom post types.

// Remove all image sizes EXCEPT for specific CUSTOM POST TYPES

add_filter('intermediate_image_sizes', function($sizes) {
    $rem = 1;
    if(in_array(get_post_type($_REQUEST['post_id']), array('CUSTOM_TYPE_1', 'CUSTOM_TYPE_2'))) $rem = 0;
    return $rem ? array() : $sizes;
}, 99);

Remove For All But Regular Posts And a Specific Custom Post Type

The following removes all images sizes for everything except regular posts and one custom post type.

On line 4, change CUSTOM_POST_TYPE to your own custom post type.

// Remove all image sizes EXCEPT for regular posts or a CUSTOM_POST_TYPE
add_filter('intermediate_image_sizes', function($sizes) {
	$rem = 1;
	if(in_array(get_post_type($_REQUEST['post_id']), array('post', 'CUSTOM_POST_TYPE'))) $rem = 0;
	return $rem ? array() : $sizes;
}, 99);

Remove For a Specific Category And All Custom Post Types Except One

This example removes all image sizes in WordPress for a specific category, and for all custom post types except for one specified custom post type.

In other words, WordPress will only create extra image sizes for:

  • regular posts except for the specified category
  • the specified custom post type

So, this stops WordPress from creating extra image sizes on Pages, the category specified, and all other custom post types except for the one specified.

You Must Edit This In The Code:

On line 10, replace 123 with your category ID to remove image sizes for.

On line 14, replace CUSTOM_POST_TYPE with the custom type that should KEEP images sizes as is.

/**
* Remove all image sizes unless it's for regular post (but excluding a specified category) or a specified custom post type
*/
add_filter('intermediate_image_sizes', function($sizes) {
	$rem = 1;
	$type = get_post_type($_REQUEST['post_id']);
	
	// @todo replace 123 with category ID to remove image sizes for

	if ('post' == $type && ! in_array(123,wp_list_pluck(get_the_category($_REQUEST['post_id']), 'term_id'))) $rem = 0;

	// @todo replace CUSTOM_POST_TYPE with the custom type that should KEEP images sizes as is

	elseif('CUSTOM_POST_TYPE' == $type) $rem = 0;

	return $rem ? array() : $sizes;

}, 99);

See more:

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]