I think by default some metas are created with post creation and you are using add_post_meta instead of update_post_meta so if the meta exist, for some reason, this meta will be duplicated.
I checked update_post_meta docs and this method will update the meta if was found and if not will be created. Well if you put add_post_meta instead update_post_meta for some reason let me know.
I changed that and it works for me, no more duplicated metas in my new cloned posts.
//Add the source post meta to the destination post
foreach ( $data as $key => $values) {
foreach ($values as $value) {
update_post_meta( $post_id, $key, $value );
}
}
//Copy the meta data collected from the sourse post to the new post
foreach ($meta_values as $key => $values) {
foreach ($values as $value) {
//If the data is serialised we need to unserialise it before adding or WordPress will serialise the serialised data
//...which is bad
if(is_serialized($value)){
update_post_meta( $post_id, $key, unserialize($value));
}else{
update_post_meta( $post_id, $key, $value );
}
}
}
Thanks.
-
This reply was modified 9 years, 7 months ago by
joedev91.
Once again thanks joe. Dunno why I had that set at add_ rather than update_. I’m a silly man.