How To Duplicating WordPress Post Programmatically

I would like to duplicate existing WordPress post by given post id

You need to collect data from current WordPress post and save to new WordPress post

$oldpost = get_post($post_id);
$post    = array(
    'post_title' => $oldpost->post_title,
    'post_status' =>  $oldpost->post_status,
    'post_type' => $oldpost->post_type,
    'post_author' => $oldpost->post_author,
);
$new_post_id = wp_insert_post($post);
$data = get_post_custom($post_id);
foreach ( $data as $key => $values) {
    foreach ($values as $value) {
        add_post_meta( $new_post_id, $key, $value );
    }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments