# Generate a Custom Image Size Only for One Post Type

> Registers an image size, such as a wide portfolio banner, but only creates it for images uploaded to that post type, so every other upload is not stored in yet another size.

- Type: PHP
- Runs: Run everywhere
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: image-sizes, post-types, disk-space
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/media-image-size-for-one-post-type/

## Code

```php
// Image size and the only post type that gets it.
$wpide_type_size_name      = 'portfolio-banner';
$wpide_type_size_width     = 1600;
$wpide_type_size_height    = 700;
$wpide_type_size_post_type = 'portfolio';

add_action( 'after_setup_theme', function () use ( $wpide_type_size_name, $wpide_type_size_width, $wpide_type_size_height ) {
    add_image_size( $wpide_type_size_name, $wpide_type_size_width, $wpide_type_size_height, true );
} );

add_filter( 'intermediate_image_sizes_advanced', function ( $sizes, $metadata, $attachment_id = 0 ) use ( $wpide_type_size_name, $wpide_type_size_post_type ) {
    $parent = $attachment_id ? (int) wp_get_post_parent_id( $attachment_id ) : 0;
    if ( ! $parent && isset( $_REQUEST['post_id'] ) ) {
        $parent = absint( $_REQUEST['post_id'] );
    }

    if ( ! $parent || get_post_type( $parent ) !== $wpide_type_size_post_type ) {
        unset( $sizes[ $wpide_type_size_name ] );
    }

    return $sizes;
}, 10, 3 );
```

## Notes

The size is created when you upload from inside a post of that type (the image is "attached" to it); set the name, dimensions and post type below.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Generate a Custom Image Size Only for One Post Type" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
