Custom Fields
Re-Envisioned
Support MB Frontend Submission Deleting custom post_type with frontend submission form permanently deletes post

  • Creator
    Topic
  • #32794
    Resolved Leerpodium
    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #32799
    Long 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
    Leerpodium
    Participant

    Ok, I will do that.

    Thanks for the reply.

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