voceconnect / multi-post-thumbnails

Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
143 stars 63 forks source link

Multipost thumbnail not displaying with custom post type #99

Closed beneshort closed 8 years ago

beneshort commented 8 years ago

Hi,

Great plugin btw - it's been extremely useful!

I've been using the plugin with normal posts for a couple of months with no issues. I now have a new post type, 'recipe', that I would like to use the plugin with, and so I'm trying to update my existing registered secondary thumbnail to work on the new 'recipe' post type also. However, after doing this, I am having issues with displaying this thumbnail on any new posts.

For the past two months, in my site-specific plugin, I used this code to register my secondary thumbnail:

function register_second_featured_image() {
  if (class_exists('MultiPostThumbnails')) {
    new MultiPostThumbnails(
        array(
            'label' => 'Potrait Image',
            'id' => 'potrait-image',
            'post_type' => 'post'
        )
    );
    new MultiPostThumbnails(
        array(
            'label' => 'Video Image',
            'id' => 'video-image',
            'post_type' => 'post'
        )
    );
  }
}
add_action( 'wp_loaded', 'register_second_featured_image' );

I have now updated this to the following code to try and register the existing thumbnail with the new post type as well:

//Register additional featured images
function register_second_featured_image() {
  if (class_exists('MultiPostThumbnails')) {
    $mpt_post_types = array('post', 'recipe');
    foreach($mpt_post_types as $mpt_post_type) {
        new MultiPostThumbnails(array(
            'label' => 'Portrait Image',
            'id' => 'portrait-image',
            'post_type' => $mpt_post_type
            )
        );
        new MultiPostThumbnails(array(
            'label' => 'Video Image',
            'id' => 'video-image',
            'post_type' => $mpt_post_type
            )
        );
    }
  }
}
add_action( 'wp_loaded', 'register_second_featured_image' );

Unfortunately the new registration code appears to have registered the thumbnails as entirely new thumbnails in MultiplePostThumbnails, even though the ID is the same.

post-portrait-image-saved

new-post-portrait-image-saved

This is causing a problem when it comes to displaying the "Portrait Image" thumbnails. Even though nothing seems to have changed in the way the image is saved in the database, the image will not display for any new posts of either post type. However I can still get it to display on posts for which I uploaded a "Portrait Image" using my old code.

I've already done a fair bit of testing, and to try and boil it down to the basics, I have created a shortcode to use within my posts (so within the loop) to try and test:

//[foobar]
function test_portrait_func( $atts ){
    if (class_exists('MultiPostThumbnails')) :
        if (MultiPostThumbnails::has_post_thumbnail(get_post_type(), 'portrait-image')) {
            echo "<p>I have a portrait image</p>";
        } else {
            echo "<p>No image!</p>";
        }
        MultiPostThumbnails::the_post_thumbnail(
            get_post_type(),
            'potrait-image',
            NULL
        );
    endif;
}
add_shortcode( 'foobar', 'test_portrait_func' );

When I use this [foobar] shortcode in my posts, I get the result I mentioned above:

I've also tried manually passing the $post_type and $post_id for the new post id 3379 that has the thumbnail saved in the database (picture above), but to no avail.

Any clues what I could be doing wrong here? Or is this expected behaviour / a bug with me trying to effectively "overwrite" the existing MultiPostThumbnail ID?

Your help will be seriously appreciated 👍

beneshort commented 8 years ago

Literally as I posted this, I realised that in my old code I misspelt "potrait" as "potrait".... which will be why it's registered as a new thumbnail. I'll try and fix this shortly and get back to you if I'm still having issues, but pretty sure I can solve this now...

beneshort commented 8 years ago

Fixed. Hours spent diagnosing what turned out to be me not realising that I'd corrected my own earlier spelling mistake >.<