Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @tgilber007

    I hope you’re well today!

    Currently it’s not possible out of the box. Forminator doesn’t “know” to which post form is added so it cannot track it and do such removal.

    It might be doable with an additional custom code (though it will be “resource intensive” operation) so I’ve asked our developers to look into it and see if we could help with this.

    I’d rather not make promises at this point as I’m not sure yet how complex such code would have to be (since we are not providing custom development, some more complex tasks may fall outside of the scope of this support) but they’ll check it and if it’s possible to achieve that we’ll get back to you with solution.

    Please note: I’d appreciate some patience on this as our developers are dealing with a lot of complex tasks on daily basis and their response time may be a bit longer.

    Best regards,
    Adam

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @tgilber007,

    Our developers came up with a quick workaround. Kindly use the following mu-plugin for the same:

    add_action('forminator_form_action_delete', 'wpmudev_get_form_delete_request');
    function wpmudev_get_form_delete_request($id){
    	if( $id ){
    		wpmudev_get_posts_form_shortcode($id);
    	}
    }
    
    function wpmudev_get_posts_form_shortcode($form_id){
        global $wpdb;
    	$shortcode = '[forminator_form id="'.$form_id.'"]';
        $results = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%$shortcode%' AND post_type='post'", ARRAY_N);
    	if( $results ){
    		foreach( $results as $key => $post_ids ){
    			foreach( $post_ids as $k => $post_id ){
    				wp_delete_post( $post_id, true );
    			}
    		}
    	}
    }

    To explain things further, here in the above code, we have checked the post type as a post with the following:
    post_type='post'

    You can also change this as per your need. Also, no post status is checked so if you are also looking to delete only published posts then you would need to add one more condition.
    AND post_status='publish'

    Here is an example of the following:

    
    $results = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%$shortcode%' AND post_type='post' AND post_status='publish'", ARRAY_N);

    Once the same is added, you final code should look like this:

    add_action('forminator_form_action_delete', 'wpmudev_get_form_delete_request');
    function wpmudev_get_form_delete_request($id){
    	if( $id ){
    		wpmudev_get_posts_form_shortcode($id);
    	}
    }
    
    function wpmudev_get_posts_form_shortcode($form_id){
        global $wpdb;
    	$shortcode = '[forminator_form id="'.$form_id.'"]';
        $results = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%$shortcode%' AND post_type='post' AND post_status='publish'", ARRAY_N);
    	if( $results ){
    		foreach( $results as $key => $post_ids ){
    			foreach( $post_ids as $k => $post_id ){
    				wp_delete_post( $post_id, true );
    			}
    		}
    	}
    }

    Additional Note:
    If you see we have kept the post_type=’post’ as mentioned earlier. Now, if you want to have it work for pages, then the only change needed would be to change post type to page.
    Example: post_type='page'

    Here is step-by-step documentation to make a mu-plugin with the given code above and install it: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Should you have any doubts or need any help, please reply back to us here and we would be happy to help.

    Thank you,
    Prathamesh Palve

    Thread Starter tgilber007

    (@tgilber007)

    Thanks @wpmudev-support7 and @wpmudev-support8
    Working Perfectly

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘delete post when form deleted’ is closed to new replies.