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

(FIX): Bug with Multiple Post Types (WP 4.5+) #113

Closed dumitrub closed 7 years ago

dumitrub commented 7 years ago

(My) Original post here: https://wordpress.org/support/topic/bugfix-bug-with-multiple-post-types-wp-4-5/

I've started using this plugin in 2016 and it was working fine for my posts. I have then added a new custom post type and have modified the code in functions.php like this:

new MultiPostThumbnails( array( 'label' => 'Secondary Image', 'id' => 'secondary-image', 'post_type' => array('post','testimonial') ) );

It was working at first, but after WordPress update to 4.5.2 or 4.5.3 it stopped (half-way). The "Secondary Image" image upload was still appearing in the right places, for both Posts and Testimonial posts, but on the front-end it wasn't. It was working well with EXISTING thumbnails, but not with NEW thumbnails.

So I decided to get to the bottom of it.

In your wp_postmeta table, if you have MULTIPLE post types like I have above, the plugin creates a new row like this:

meta_id: 1111 post_id: 2222 metakey: Arraysecondary-image_thumbnail_id meta_value: 3333

So it is obvious that the plugin simply doesn't store correctly the meta_key in the table if you are using multiple post types.

The FIX to this is SIMPLE:

Go to /wp-content/plugins/multiple-post-thumbnails/multi-post-thumbnails.php and find line #444, it looks like this:

$this->set_meta($post_ID, $this->post_type, $this->id, $thumbnail_id);

change it to this: $this->set_meta($post_ID, get_post_type($post_ID), $this->id, $thumbnail_id);

The issue is that the plugin uses the custom post type that is defined in your functions.php and uses it as a string ONLY when adding the meta field to the DB, it doesn't check if it is an array or a string.

This fixes the issue, but you will have to re-apply your featured images to the posts in which it wasn't working (as it wasn't saving them correctly).

dumitrub commented 7 years ago

Later add: actually this fix is not sufficient to make everything work. The thumbnail will start appearing on the front-end, but now it disappears from the back-end, making it impossible to then Remove the secondary image.

Will investigate further.

dumitrub commented 7 years ago

OK, was able to properly update the plugin with a proper fix, it now works perfectly with a single or multiple post types. The full (updated) code is here: http://pastebin.com/9BFvcb5q

kevinlangleyjr commented 7 years ago

@dumitrub - Can you please submit a pull request with these changes and reference this issue?

dumitrub commented 7 years ago

I'm new to GitHub, I hope I did it right :)

chrisscott commented 7 years ago

Thanks for the feedback and PR.

The plugin wasn't intended to support specifying post types as an array so that it worked at one point is a side effect and wasn't intended in the design or in the docs. I'd prefer to keep this behavior and, if anything, add a check for the post_type arg being a string and showing an error like we do for missing args if it isn't.

kevinlangleyjr commented 7 years ago

@dumitrub - you could do the following to satisfy your needs without any changes to the plugin.

foreach( array( 'post', 'testimonial' ) as $post_type ){
    new MultiPostThumbnails( array(
        'label' => 'Secondary Image',
        'id' => 'secondary-image',
        'post_type' => $post_type
    ) );
}