Created
September 16, 2015 09:35
-
-
Save eduwass/4825a88f5342bbbc6d4a to your computer and use it in GitHub Desktop.
Programmatically copy over the content of another post in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Duplicates a post & its meta into another | |
* @param [int] $post_id The Post you want to clone | |
* @param [int] $destination_id The Post where you want to copy to | |
*/ | |
function copy_post($post_id, $destination_id) { | |
$title = get_the_title($post_id); | |
$oldpost = get_post($post_id); | |
$post = array( | |
'ID' => $destination_id, | |
'post_title' => $title, | |
'post_status' => 'publish', | |
'post_type' => $oldpost->post_type, | |
'post_author' => 1 | |
); | |
wp_update_post($post); | |
// Copy post metadata | |
$data = get_post_custom($post_id); | |
foreach ( $data as $key => $values) { | |
foreach ($values as $value) { | |
update_post_meta( $destination_id, $key, $value ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment