0

What I'm trying to achieve is to define a specific image size (a portrait thumbnail) for a specific post type (books), so that only this post type has this thumbnail size generated and I'm not wasting disk space by creating this thumbnail format for all the other post types present on the site.

I am trying to follow the logic of this answer to a similar question and this is the code I came up so far:

// Delete all intermediate image sizes
add_filter( 'intermediate_image_sizes', '__return_empty_array', 99 );

// Add custom image sizes depending on post type
function add_custom_post_types_image_sizes( $metadata, $attachment_id ) {

  $post_parent_id = wp_get_post_parent_id( $attachment_id );

  // For books
  if ( 'book' === get_post_type( $post_parent_id ) ) {

    // Add portrait thumbnail
    add_image_size( 'portrait_thumbnail', '150', '215', true );

    // This doesn't work :
    $file = get_attached_file( $attachment_id );
    wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );

  }

  return $metadata;

}

add_filter( 'wp_generate_attachment_metadata', 'add_custom_post_types_image_sizes', 10, 2 );
  • All images uploaded go into the media library and can be used anywhere else in WordPress. This means that all images need to be available in all sizes. In WordPress it doesn't make sense to say that images are "for" a specific post type. It might make sense for business reasons, but there's nothing in WordPress that technically enforces this. If your hosting is so stingy that an extra 150x215 image size is causing you problems, find a new host. – Jacob Peattie Aug 2 at 1:01
  • The only real solution to this problem other than that is to add a server for on-the-fly image optimisation, but that's beyond the scope of a single answer. However there are plugins available, like Jetpack's Photon or Smush CDN, that support this. – Jacob Peattie Aug 2 at 1:01
0

You can try this (not tested):

// Add image sizes for book outside
add_image_size( 'book_portrait_thumbnail', '150', '215', true );
add_image_size( 'book_portrait_square', '300', '300', true );

// Delete all intermediate image sizes
add_filter( 'intermediate_image_sizes', 'custom_image_sizes', 99 );
function custom_image_sizes( $image_sizes ) {

    //In case of book return book sizes array 
    if( isset($_REQUEST['post_id']) && 'books' === get_post_type( $_REQUEST['post_id'] ) ){
        return array( 'book_portrait_thumbnail', 'book_portrait_square' );  
    }

    // Otherwise the other ones
    return $image_sizes;  

}

Of course, this is for science sake. Like others suggested I would rather find a different hosting provider, this is not relevant for the current state of tech offer.

  • 1
    Keep in mind that this means that if you upload an image anywhere other than directly to the post, and assign it as the featured image, it will be missing these sizes. – Jacob Peattie Aug 2 at 1:02
  • Yes, true, but the current case is on whether is possible or not to achieve and save disk space. I agree with you, I wouldn't do that. it's not relevant for the current general hosting offer often giving unlimited (practically) storing space. – huraji Aug 2 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.