_wp_copy_post_meta()WP 6.4.0

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

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Return

null. Nothing (null).

Usage

_wp_copy_post_meta( $source_post_id, $target_post_id, $meta_key );
$source_post_id(int) (required)
Post ID to copy meta value(s) from.
$target_post_id(int) (required)
Post ID to copy meta value(s) to.
$meta_key(string) (required)
Meta key to copy.

Changelog

Since 6.4.0 Introduced.

_wp_copy_post_meta() code WP 6.6.2

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 ) );
	}
}