First of all we need to find the posts related to our taxonomy term. Then we will delete them one by one by running a foreach
loop. Try below code. I hope that will work for you-
add_action('pre_delete_term', 'the_dramatist_delete_posts_on_taxonomy_delete', 1, 1 );
function the_dramatist_delete_posts_on_taxonomy_delete( $term ) {
$args = array(
'post_type' => 'post', // post_type
'tax_query' => array(
array(
'taxonomy' => 'post_tag', // taxonomy_name
'field' => 'id',
'terms' => $term
)
)
);
$posts = get_posts($args);
foreach ( $posts as $post ){
wp_delete_post( $post->ID, true );
}
}