• Resolved pipoulito

    (@pipoulito)


    I added this code in function.php in order to replace post_date with an ACF field every time i update mu custom post. It works perfectly !

    BUT

    Now in WP admin i can’t delete (to trash) any custom post in the list

    Could someone help me please ?
    thanks

    `
    add_action(‘save_post’, ‘change_content’);

    global $post;
    global $wpdb;
    function change_content($post_id) {
    if(get_post_type($post_type) == ‘mycustompost’ && get_field (‘dates_0_date_spectacle’) != ” )
    {
    $datefield = get_post_meta($post_id,’dates_0_date_spectacle’,true);
    $post_date = date(“Y-m-d H:i:s”, strtotime($datefield));
    $my_post = array();
    $my_post[‘ID’] = $post_id;
    $my_post[‘post_date’] = $post_date;
    $my_post[‘post_status’] = ‘publish’;
    remove_action(‘save_post’, ‘change_content’);
    wp_update_post( $my_post );
    add_action(‘save_post’, ‘change_content’);
    }
    }
    `

Viewing 2 replies - 1 through 2 (of 2 total)
  • Every time you are saving the post (pretty sure trashing also is “saving”) you are updating the post and setting the post status to publish. You could probably just remove the post status line from your update arguments, or if you need that then you need to add some conditionals around it.

    I bet if you just tried to draft a post it would do the same thing.

    Thread Starter pipoulito

    (@pipoulito)

    oh yes of course !
    removing $my_post[‘post_status’] = ‘publish’;
    IT WORKS
    thanks a lot !!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Can't delete my custom post after using save-post’ is closed to new replies.