• Resolved brushy111

    (@brushy111)


    There is a bug in this plugin in the Utilities.php file, Utilities class, copyMeta function, line 149.

    Here is the code in question (lines 145 to 156):

    foreach( $sourcePostMeta as $key => $value ){
    
    	if( ! in_array( $key, $blackListKeys ) ){
    
    		$value = is_array( $value ) ? reset( $value ) : $value;     //  We need single value.
    		$value = maybe_unserialize( $value );
    
    		update_post_meta( $targetPostId, $key, wp_slash( $value ) );
    
    	}
    
    }

    The problem is that it is valid for a post to have multiple metadata rows with the same meta_key value. That’s why WordPress by default returns an array from the function get_post_meta(); you have to explicitly tell it if you want a single value. What that means for the copyMeta function is that when a post has multiple meta_values for a single meta_key, the code above will delete all but the first value found (Not good!).

    Also note that when the get_post_meta() function is passed ” for the 2nd parameter ($key), it always returns all data as arrays. So setting the 3rd parameter ($single) to true will not do anything. Since all meta_values are returned as arrays, you can just loop through them all and call add_post_meta on each item in each array.

    Please replace the code above with something like this:

    
    foreach( $sourcePostMeta as $key => $values ){
    
    	if( ! in_array( $key, $blackListKeys ) ){
    
    		foreach( $values as $value ) {
    
    			$value = maybe_unserialize( $value );
    
    			add_post_meta( $targetPostId, $key, wp_slash( $value ) );	
    
    		}
    
    	}
    
    }
    

    I hope you can fix this quickly for us!

    Thank you,

    Zach

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Bug in Utilities->copyMeta function, line 149’ is closed to new replies.