Skip to content

Instantly share code, notes, and snippets.

@eduwass
Created September 16, 2015 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduwass/4825a88f5342bbbc6d4a to your computer and use it in GitHub Desktop.
Save eduwass/4825a88f5342bbbc6d4a to your computer and use it in GitHub Desktop.
Programmatically copy over the content of another post in Wordpress
<?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