plugins - Keep a post copy when this one is updated - issue

I try to keep a copy of the previous version a custom post when this one is updated. It works, but duplicate the previou

I try to keep a copy of the previous version a custom post when this one is updated. It works, but duplicate the previous version twice. Help appreciated !

DUPLICATE FUNCTION :

function duplicate($post_id) {
$title   = get_the_title($post_id);
$oldpost = get_post($post_id);
$post    = array(
  'post_title' => $title,
  'post_status' => 'draft',
  'post_date' => $oldpost->post_date,
  'post_type' => $oldpost->post_type
);

$new_post_id = wp_insert_post($post);


$meta = get_post_meta($post_id);
foreach ( $meta as $key => $values) {
  foreach ($values as $value) {
    update_post_meta( $new_post_id, $key, $value );
  }
}
// GALLERY
$gal = get_field('field_5ebbe9729c82d', $post_id);
update_field('field_5ebbe9729c82d', $gal, $new_post_id);


return $new_post_id; }

SAVE WITH A NEW DATE

function modify_post_date( $post_id, $post, $update )
{   
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { return; }
if( defined( 'DOING_AUTOSAVE' ) && 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 ); 

    $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 ); 

SAVE PREVIOUS VERSION

function _duplicate($post_id, $post_after, $post_before){

$p_status = get_post_status($post_id); 
$p_type = get_post_type($post_id); 

if ( defined( 'DOING_CRON' ) && DOING_CRON ) { return; }
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )  { return; }
if (wp_is_post_autosave($post_id)) { return; }
if ( !current_user_can( 'edit_post', $post_id ) ) { return; }
if( $p_type !== 'armoire' ) { return; }
if( wp_is_post_revision( $post_id ) ) { return; }


if( $p_status == 'publish' ) {

    remove_action( 'post_updated', '_duplicate', 50, 3 );

    duplicate($post_before);

    remove_action( 'post_updated', '_duplicate', 50, 3 );
}
}

add_action( 'post_updated', '_duplicate', 50, 3 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742409746a4438571.html

相关推荐

  • plugins - Keep a post copy when this one is updated - issue

    I try to keep a copy of the previous version a custom post when this one is updated. It works, but duplicate the previou

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信