Skip Navigation

[Resolved] Delete Repeating Field Group Attachments when Deleting Parent Post

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 2 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2318905

We have a job application portal on our website where each application is saved as an item within the RFG of the parent post. The slug of the parent post is careers and the slug of the RFG is candidate-info. I am trying to write some PHP code that will delete all the attachments for the candidate info RFG while deleting the parent careers post. The slug of the parent post has been changed a few times in the past so I am not able to pin down what exactly to use in the code.

I modified my code from this : https://toolset.com/forums/topic/delete-related-posts-and-their-attachments-when-deleting-parent-post/

To this:

function delete_candidate_info($post_id) {
	global $wpdb;
	$post_type = get_post_type( $post_id );
	if ( $post_type == 'careers' || $post_type == 'candidate-info') {
		$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 == 'careers' ) {
			// in this case, we want to force-delete the child product posts as well
			$career_candidates = toolset_get_related_posts(
				$post_id,
				'careers-candidate-info',
				[
					'query_by_role' => 'parent',
					'role_to_return' => 'child',
					'limit' => 10000,
					'orderby' => null,
				]
			);
			foreach($career_candidates as $career_candidate ) {
				wp_delete_post( $career_candidate, true );
			}
		}
	}
}
add_action('before_delete_post', 'delete_candidate_info');

Here is the link to a sample careers post with the form below set to add entries to the candidate info RFG.

hidden link

#2319543

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