Merge posts plugin?

Does anyone know of a plugin that is capable of merging the post meta from one post into another?

I’m working on re-releasing the Driftwood contact manager theme and I’m trying to find a way to solve the duplicate contact issue. Obviously this question could impact far more people than just myself. 🙂

Related posts

Leave a Reply

1 comment

  1. /**
     * 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;
    }