2021-01-04 11:33:00

Related Docs

Do You Need Help or Expertise?

Is editing Source Code not Your Thing? Are you in a tight Time Schedule and need to deliver a fully scalable and safe solution fast to your Clients?

Maybe you want to incorporate the Software found on TukuToi unbranded and royalty free in your Clients Projects after customizing it as per your Projects requirements?

Or eventually you need some training on any WordPress Plugin, Theme or their creation and maintenance?

FilterAutomatically delete child posts when parent is deleted

Delete Related Posts when deleting a Post in a Types Relationship

The following is intended to work with relationships created with Types 3+. Edit the code to include the slug of the parent post type, and the slug of the relationship. When a parent post is deleted, its children will be deleted, too.

add_action( 'delete_post', 'ts_delete_children', 1, 20 );
function ts_delete_children( $post_id, $post ){

    $parent = 'project'; // Edit parent post type slug

    $relationship = 'project-task'; // Edit relationship slug

    if ( $post->post_type == $parent )
    {
        //get child tasks
        $children = toolset_get_related_posts( $post_id, $relationship, array('query_by_role' => 'parent','role_to_return' => 'child') );

        if ( is_array( $children ) )
        {
            foreach ($children as  $child) 
            {
                wp_delete_post( $child );
            }
        }
    }
}

Consider using the before_delete_post hook if trashing parent posts rather than directly or force deleting them.