W3cubDocs

/WordPress

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

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

Version Description
6.4.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/_wp_copy_post_meta