_wp_copy_post_meta( int $source_post_id, int $target_post_id, string $meta_key )

In this article

Copy post meta for the given key from one post to another.

Parameters

$source_post_idintrequired
Post ID to copy meta value(s) from.
$target_post_idintrequired
Post ID to copy meta value(s) to.
$meta_keystringrequired
Meta key to copy.

Source

function _wp_copy_post_meta( $source_post_id, $target_post_id, $meta_key ) {

	foreach ( get_post_meta( $source_post_id, $meta_key ) as $meta_value ) {
		/**
		 * We use add_metadata() function vs add_post_meta() here
		 * to allow for a revision post target OR regular post.
		 */
		add_metadata( 'post', $target_post_id, $meta_key, wp_slash( $meta_value ) );
	}
}

Changelog

VersionDescription
6.4.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.