Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Programmatically duplicating a WordPress post
<?php
/**
* Duplicates a post & its meta and it returns the new duplicated Post ID
* @param [int] $post_id The Post you want to clone
* @return [int] The duplicated Post ID
*/
function duplicate($post_id) {
$title = get_the_title($post_id);
$oldpost = get_post($post_id);
$post = array(
'post_title' => $title,
'post_status' => 'publish',
'post_type' => $oldpost->post_type,
'post_author' => 1
);
$new_post_id = wp_insert_post($post);
// Copy post metadata
$data = get_post_custom($post_id);
foreach ( $data as $key => $values) {
foreach ($values as $value) {
add_post_meta( $new_post_id, $key, $value );
}
}
return $new_post_id;
}
@entwane37

This comment has been minimized.

Copy link

@entwane37 entwane37 commented May 22, 2020

Thanks for this code.
I try to automatically duplicate a post when it's published, but it's duplicated twice.
An idea ?

Thanks in advance,

@entwane37

This comment has been minimized.

Copy link

@entwane37 entwane37 commented May 22, 2020

function modify_post_date( $post_id, $post, $update ) {
if( defined( 'DOING_AUTOSAVE' ) and DOING_AUTOSAVE ) { return; }
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return; }
if (wp_is_post_autosave($post_id)) { return; }
if( !$update ) { return; }
if ( !current_user_can( 'edit_post', $post_id ) ) { return; }
if( $post->post_type !== 'armoire' ) { return; }
if( wp_is_post_revision( $post_id ) ) { return; }
if( $post->post_status == 'publish' ) {
remove_action( 'save_post' , 'modify_post_date' , 10, 3 );
duplicate($post_id);
$new_date = $post->post_modified;
$old_date = $post->post_date;
$title = $post->post_title;
$current_user = wp_get_current_user();
$author = $current_user->ID;
wp_update_post( array( 'post_date' => $new_date ));
remove_action( 'save_post' , 'modify_post_date' , 10, 3 );
}
}
add_action( 'save_post' , 'modify_post_date' , 10, 3 );
?>

@AkramiPro

This comment has been minimized.

Copy link

@AkramiPro AkramiPro commented Aug 4, 2021

it is important to unserialize data to avoid conflicts:

<?php

  /**
   * Duplicates a post & its meta and it returns the new duplicated Post ID
   * @param  [int] $post_id The Post you want to clone
   * @return [int] The duplicated Post ID
   */
  function duplicate($post_id) {
    $title   = get_the_title($post_id);
    $oldpost = get_post($post_id);
    $post    = array(
      'post_title' => $title,
      'post_status' => 'publish',
      'post_type' => $oldpost->post_type,
      'post_author' => 1
    );
    $new_post_id = wp_insert_post($post);
    // Copy post metadata
    $data = get_post_custom($post_id);
    foreach ( $data as $key => $values) {
      foreach ($values as $value) {
        add_post_meta( $new_post_id, $key, maybe_unserialize( $value ) );// it is important to unserialize data to avoid conflicts.
      }
    }

    return $new_post_id;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment