Deleting custom post_type with frontend submission form permanently deletes post

Support MB Frontend Submission Deleting custom post_type with frontend submission form permanently deletes postResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #32794
    LeerpodiumLeerpodium
    Participant

    Hi

    Deleting a post with a custom post_type using MB frontend submission form permanently deletes the post. Even when force_delete = "false".

    I see that the MBFS\Form class calls the following method when deleting posts by ID.

     public function delete() {
            if ( empty( $this->config['post_id'] ) ) {
                return;
            }
    
            $force_delete = 'true' === $this->config['force_delete'];
    
            do_action( 'rwmb_frontend_before_delete', $this->config );
            wp_delete_post( $this->config['post_id'], $force_delete );
            do_action( 'rwmb_frontend_after_delete', $this->config, $this->config['post_id'] );
        }

    So the wp_delete_post() function is always called.
    Inside the wp_delete_post() function WordPress only reverts to wp_trash_post() when post_type == 'post' or post_type == 'page'.

        if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
            return wp_trash_post( $postid );
        }

    I understand that I could add an action to "rwmb_frontend_before_delete" hook and trash the post myself. But that would mean extra code and overhead.

    Would it be possible to avoid calling wp_delete_post() for custom post_types? Maybe by calling wp_trash_post() instead of wp_delete_post() when for_delete="false"?

    Cheers and thanks in advance.

    Tako

    #32799
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use this filter pre_delete_post to check the post type and return the function wp_trash_post(). Here is the example:

    add_filter( 'pre_delete_post', function( $null, $post, $force_delete ) {
        if( 'job' == $post->post_type && $force_delete == false ) {
            return wp_trash_post( $post->ID );
        }
        return $null;
    }, 10, 3 );

    Source https://github.com/WordPress/WordPress/blob/master/wp-includes/post.php#L3392

    #32800
    LeerpodiumLeerpodium
    Participant

    Ok, I will do that.

    Thanks for the reply.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.