Support » Plugins and Hacks » Duplicate Post » update_post_meta tweak?

  • Within the duplicate_post_copy_post_meta_info function, you’ve got this:

    /**
     * Copy the meta information of a post to another post
     */
    function duplicate_post_copy_post_meta_info($new_id, $post) {
    	$post_meta_keys = get_post_custom_keys($post->ID);
    	if (empty($post_meta_keys)) return;
    	$meta_blacklist = explode(",",get_option('duplicate_post_blacklist'));
    	if ($meta_blacklist == "") $meta_blacklist = array();
    	$meta_keys = array_diff($post_meta_keys, $meta_blacklist);
    
    	foreach ($meta_keys as $meta_key) {
    		$meta_values = get_post_custom_values($meta_key, $post->ID);
    		foreach ($meta_values as $meta_value) {
    			$meta_value = maybe_unserialize($meta_value);
    			add_post_meta($new_id, $meta_key, $meta_value);
    		}
    	}
    }

    Would you be opposed to modifying it to use update_post_meta like this?

    /**
     * Copy the meta information of a post to another post
     */
    function duplicate_post_copy_post_meta_info($new_id, $post) {
    	$post_meta_keys = get_post_custom_keys($post->ID);
    	if (empty($post_meta_keys)) return;
    	$meta_blacklist = explode(",",get_option('duplicate_post_blacklist'));
    	if ($meta_blacklist == "") $meta_blacklist = array();
    	$meta_keys = array_diff($post_meta_keys, $meta_blacklist);
    
    	foreach ($meta_keys as $meta_key) {
    		$meta_values = get_post_custom_values($meta_key, $post->ID);
    		foreach ($meta_values as $meta_value) {
    			$meta_value = maybe_unserialize($meta_value);
    
    			if ( 1 == count( $meta_values ) )
    				update_post_meta( $new_id, $meta_key, $meta_value );
    			else
    				add_post_meta( $new_id, $meta_key, $meta_value );
    		}
    	}
    }

    Or perhaps, just using delete_post_meta for each $meta_key like this?

    /**
     * Copy the meta information of a post to another post
     */
    function duplicate_post_copy_post_meta_info($new_id, $post) {
    	$post_meta_keys = get_post_custom_keys($post->ID);
    	if (empty($post_meta_keys)) return;
    	$meta_blacklist = explode(",",get_option('duplicate_post_blacklist'));
    	if ($meta_blacklist == "") $meta_blacklist = array();
    	$meta_keys = array_diff($post_meta_keys, $meta_blacklist);
    
    	foreach ($meta_keys as $meta_key) {
    		delete_post_meta( $post->ID, $meta_key );
    		$meta_values = get_post_custom_values($meta_key, $post->ID);
    		foreach ($meta_values as $meta_value) {
    			$meta_value = maybe_unserialize($meta_value);
    			add_post_meta($new_id, $meta_key, $meta_value);
    		}
    	}
    }

    By doing either of these (delete_post_meta would probably be preferred), you should be safe from plugin conflicts that hook into save_post to add their own meta, effectively preventing duplicate keys (since this is a straight duplicate of all keys not blacklisted)

    http://wordpress.org/extend/plugins/duplicate-post/

Viewing 1 replies (of 1 total)
  • Plugin Author Lopo

    (@lopo)


    Hi, sorry for the delay in answering the questions here.

    I’ll check your suggestions as soon as I resume work on the plugin, hopefully during the holidays.

    Thanks, all these suggestions and bug reports are very helpful!

Viewing 1 replies (of 1 total)
  • The topic ‘update_post_meta tweak?’ is closed to new replies.