• Resolved bradvin

    (@bradvin)


    hi there,

    I was looking at your code to see how you clone/duplicate posts because I wanted to do something similar in my plugin.

    I noticed an issue when posts are cloned with serialized post meta, or when the post meta was stored as an array (which is serialized by WP).

    By adding a small check when you loop through the meta, it will also handle serialized arrays correctly:

    foreach ( $meta as $key => $val ) {
      $meta_value = $val[0];
      if ( is_serialized( $meta_value ) ) {
        $meta_value = @maybe_unserialize( $meta_value );
      }
    
      update_post_meta( $new_id, $key, $meta_value );
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author George Pattichis

    (@pattihis)

    Hello @bradvin

    You are right, there might be cases where the post meta value should be unserialized before being copied over.

    Your conditional though is redundant imo, since that’s exactly what maybe_unserialize does in the core.

    I’ve just pushed an update based on your improvement suggestion mentioning you.

    Thanks a lot for reporting this, cheers!

    Thread Starter bradvin

    (@bradvin)

    thanks for doing this so quickly @pattihis

    You are right the condition is redundant 🙂 I have also updated my code, so thanks

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Cloning Posts with serialized post meta’ is closed to new replies.