/**
* Merge metadata from one post to another.
*
* @param int $from_ID Source post ID
* @param int $to_ID Target post ID
* @param bool $overwrite Whether to overwrite metadata if the key already exists
* @return bool|array
*/
function wpse_20231_merge_postmeta( $from_ID, $to_ID, $overwrite = true )
{
// get ALL metadata for $from_ID
if ( !$source_meta = get_metadata( \'post\', $from_ID ) )
return false;
if ( !$existing_meta = get_metadata( \'post\', $to_ID ) )
$existing_meta = array();
// loop over source meta and update, depending on if overwrite is true & existing meta exists
foreach ( $source_meta as $key => $value ) {
if ( $overwrite ? true : !isset( $existing_meta[ $key ] ) )
update_post_meta( $to_ID, $key, maybe_unserialize( $value ) );
}
// return new meta data
// using plus is like array merge, but from left to right - proceding duplicate keys are ignored
return $overwrite ? $source_meta + $existing_meta : $existing_meta + $source_meta;
}