• Hi again πŸ™‚

    As plugin users are having problem setting thumbnails because this plugin is overriding the post thumbnail. I have wrote a fix for this.

    This fix sets featured image in following condition

    1. When thumbnail is enabled.
    2. When post doesn’t already have a thumbnail. This fixes issue lots of people having.

    # Fix

    class-categoryfeaturedimage.php

    
    
    	/** ==================================================
    	 * Update thumbnail meta data of a post to category thumbnail.
    	 * Only updates if post doesn't have a thumbnail image set by user.
    	 *
    	 * @param int $post_id  post_id.
    	 * @since 2.00
    	 */
    	public function update_thumbnail( $post_id ) {
    
    		// Skip if current theme doesn't support post thumbnails
    		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
    			return;
    		}
    
    		// Skip post thumbnail update if thumbnail is already set in the post
    		if ( has_post_thumbnail( $post_id ) ) {
    			return;
    		}
    
    		$term_id = $this->choose_term( $post_id );
    		if ( 0 < $term_id ) {
    			$featured_image_id = intval( get_term_meta( $term_id, 'featured_image_id', true ) );
    			update_post_meta( $post_id, '_thumbnail_id', $featured_image_id );
    		} else {
    			delete_post_meta( $post_id, '_thumbnail_id' );
    		}
    
    	}
    

    Here is a gist url for your continence!
    https://gist.github.com/prionkor/5207a953fbc9590acf104431f74719e2

    I have tested it but please test yourself as well. Let me know if you have any comments.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    Thanks your contribution!
    I can’t see it because I’m busy with other things I’m currently working on.
    Please wait for a while.

    Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    Good !

    // Skip if current theme doesn't support post thumbnails
    if ( ! current_theme_supports( 'post-thumbnails' ) ) {
    	return;
    }

    Not so good.

    // Skip post thumbnail update if thumbnail is already set in the post
    if ( has_post_thumbnail( $post_id ) ) {
    	return;
    }

    This is because when you change the category, the thumbnails don’t follow.

    Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    You have been added to Committers.

    Thread Starter prionkor

    (@prionkor)

    Then problem is everyone having because the code is deleting featured images in else section.

    
    delete_post_meta( $post_id, '_thumbnail_id' );
    

    Could you let me know what feature you are trying to do here. Probably I can come up with a solution.

    Probably we should only run code below when we find featured image id === post thumbnail id?

    
    $term_id = $this->choose_term( $post_id );
    		if ( 0 < $term_id ) {
    			$featured_image_id = intval( get_term_meta( $term_id, 'featured_image_id', true ) );
    			update_post_meta( $post_id, '_thumbnail_id', $featured_image_id );
    		} else {
    			delete_post_meta( $post_id, '_thumbnail_id' );
    		}
    
    
    Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    featured image id === post thumbnail id

    In fact, I haven’t had any problems with my site at all. I believe the problem with thumbnails being removed is pre-2.00.
    I don’t know why everyone is buzzing about it in the support thread.
    So I don’t think I need to change most of them.

    What about on your site?
    I’d appreciate it if you could describe a specific example.
    If you have a problem, try to commit.

    Hi, I think that I have find the right solution. Probably can be optimized, but the special part is that check “if ( has_post_thumbnail( get_queried_object_id()) ) {”

    	/** ==================================================
    	 * Update thumbnail meta data of a post to category thumbnail.
    	 * Only updates if post doesn't have a thumbnail image set by user.
    	 *
    	 * @param int $post_id  post_id.
    	 * @since 2.00
    	 */
    	public function update_thumbnail( $post_id ) {
    
    		// Skip if current theme doesn't support post thumbnails
    		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
    			return;
    		}
    
    		// Skip post thumbnail update if thumbnail is already set in the post
    		if ( has_post_thumbnail( $post_id ) ) {
    			return;
    		}
    
    		// Need to check original post ID (not version)
    		if ( has_post_thumbnail( get_queried_object_id()) ) {
    			return;
    		}
    		
    		$term_id = $this->choose_term( $post_id );
    		if ( 0 < $term_id ) {
    			$featured_image_id = intval( get_term_meta( $term_id, 'featured_image_id', true ) );
    			update_post_meta( $post_id, '_thumbnail_id', $featured_image_id );
    		} else {
    			delete_post_meta( $post_id, '_thumbnail_id' );
    		}
    
    	}
    • This reply was modified 3 years, 5 months ago by wiploo.

    Hi
    thanks for the fix. There is still one big problem: if I set a featured image for a single post, then I change category to the post (I’ve done it via the post list page in WP dashboard) it overrides the image, and I find all my posts to have the default category image. This is terrible, since it deletes hours of work!

    Thanks
    davide

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘bugFix for Thumbnail image issue’ is closed to new replies.