Skip to content

Instantly share code, notes, and snippets.

@ruman
Last active April 15, 2021 09:14
Show Gist options
  • Save ruman/e41a4e90d65a0127f3bc103d7d2992c7 to your computer and use it in GitHub Desktop.
Save ruman/e41a4e90d65a0127f3bc103d7d2992c7 to your computer and use it in GitHub Desktop.
Remove custom table data when post deleted or updated.
/*
Delete data from custom_table upon deleting the post
*/
add_action( 'admin_init', 'custom_table_admin_init' );
function custom_table_admin_init() {
add_action( 'delete_post', 'custom_table_data_delete', 10 );
}
function custom_table_data_delete( $pid ) {
global $wpdb;
$table_name = $wpdb->prefix."custom_table";
$query = $wpdb->prepare( "SELECT post_id FROM `$table_name` WHERE post_id = %d", $pid );
$var = $wpdb->get_var( $query );
if ( $var ) {
$query2 = $wpdb->prepare( "DELETE FROM `$table_name` WHERE student_id = %d", $pid );
$wpdb->query( $query2 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment