Clone custom fields from one post to another

So you can do $meta_values = get_post_meta( $post_id, $key, $single ); to grab the meta values from a post..

But how do you use $meta_values to apply them to another post? I'm not sure how you use $meta_values in add_post_meta. I am trying to copy all the custom fields from one post_id, and applying them to another post_id.

I should mention that the post that is getting a copy of the custom fields is a custom post type, and the source post is a regular post.

Thanks.

  • Tyler Postle

    Hey Chippy,

    Hope you're doing well today and thanks for your question!

    So you have custom fields for just 1 post ID? Or they are for your posts and then you want to move those custom fields so they are also available on your custom post type too? Or are you wanting to just show the values of that one post id's custom fields on another post?

    Hopefully I'm not making this more confusing!

    Look forward to hearing back Chippy.

    Cheers,
    Tyler

    • Chippy

      Hey Tyler,

      I was a little confused by what you were asking.

      I have 2 post ID's that are already set, they are $source_post_id and $destination_post_id.

      I want to copy all the custom fields from the single post, from $source_post_id in an array.

      I then want to bring all the custom fields from that post into $destination_post_id.

      $source_post_id is a normal wordpress post that uses normal custom fields.

      $destination_post_id is a custom wordpress post that uses normal custom fields as well (no custom taxonomy/custom fields/shortcodes).

      I figure using $meta = get_post_meta with the source post ID, that there is a single command to move $meta (all the custom fields) into another single post id? I do the switch_to_blog and all the other stuff in the background, I just need to know how to apply $meta to another post_id. All the guides I see only explain how to move add or update a single field. I want to move the entire array created from get_post_meta.

      I just think that there is an easier way to do this:

      $custom_field1 = get_post_meta( $source_post_id, $custom_field1, true );
      $custom_field2 = get_post_meta( $source_post_id, $custom_field2, true );
      $custom_field3 = get_post_meta( $source_post_id, $custom_field3, true );
      etc...
      add_post_meta( $destination_post_id, 'custom_field1', $custom_field1 );
      add_post_meta( $destination_post_id, 'custom_field2', $custom_field2 );
      add_post_meta( $destination_post_id, 'custom_field3', $custom_field3 );
      etc...

      Thanks.

  • DavidM

    Hi @Chippy,

    Thanks for your explanation! Have you considered using foreach to add the items from the array, maybe like so:

    $custom_fields = get_post_custom( $source_post_id );
    foreach ( $custom_fields as $key => $value ) {
      add_post_meta( $destination_post_id, $key, $value );
    }

    Not sure if you're already using get_post_custom() but here's a reference:
    http://codex.wordpress.org/Function_Reference/get_post_custom

    Maybe something like that would work for you?

    Cheers,
    David

  • Chippy

    Sorry, it took me some time to get back.

    I think I found an error in your solution, @DavidM

    $value returns as an array, and therefore has to be changed to $value[0]

    For anyone else who finds this article, the full, corrected code that works for me is:

    $custom_fields = get_post_custom( $source_post_id );
    foreach ( $custom_fields as $key => $value ) {
      add_post_meta( $destination_post_id, $key, $value[0] );
    }

    Thanks for the help though!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.