Hi,
Thank you for contacting us and I'd be happy to assist.
The code that you referenced no longer works because, by the time the related RFG entries are searched for deletion, their relationship connection with the parent post (that is being deleted) is removed.
We have an internal ticket to improve this in future releases so that the RFG entries for the permanently deleted parent posts are automatically deleted too, however, I don't have a time estimate for that change.
For now, you can use slightly updated code that adopts a different approach. Whenever parent post type ('careers') is deleted, the code cycles through all the RFG element posts ('candidate-info') and if it doesn't have a related parent post, it is deleted too:
function delete_candidate_info($post_id) {
$parent_post_slug = 'careers';
$child_post_slug = 'candidate-info';
$relationship_slug = 'candidate-info';
global $wpdb;
$post_type = get_post_type( $post_id );
if ( $post_type == $parent_post_slug || $post_type == $child_post_slug ) {
$ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'");
foreach ( $ids as $id ) {
wp_delete_attachment($id, true);
}
if(has_post_thumbnail( $post_id )) {
$tn_id = get_post_thumbnail_id( $post_id );
wp_delete_attachment($tn_id, true);
}
if( $post_type == $parent_post_slug ) {
$args = array(
'post_type' => $child_post_slug,
'posts_per_page' => -1,
'post_status' => 'publish',
);
$posts_array = get_posts( $args );
foreach ($posts_array as $post) {
$current_post_ID = $post->ID;
$parent_post = toolset_get_related_posts( $current_post_ID, $relationship_slug, 'child', 9999, 0, array(), 'post_id', 'parent' );
if(empty($parent_post)) {
wp_delete_post( $current_post_ID, true );
}
}
}
}
}
add_action('before_delete_post', 'delete_candidate_info');
Note: Please make sure that "$parent_post_slug", "$child_post_slug", and "$relationship_slug" have the correct slugs, as used on your website.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar