Skip Navigation

[Resolved] How do I delete Repeatable Group posts if they do not appear in the admin area?

This support ticket is created 2 years, 8 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 2 replies, has 2 voices.

Last updated by Bobby339 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2351041
stories-services.png

Hello! I have a CPT called "Stories" and there is a Repeatable Group Field called "Services" and my question is: I deleted a "Story" that I had made however the "Services" that were associated with that still remain in the site (I have a View that is showing them) so how do I delete the Repeatable Group Posts when the parent post is deleted? The problem is that there is no wp-admin menu entry for "Services" for me to delete them from.

#2351379

Hi,

Thank you for contacting us and I'd be happy to assist.

Your observation is correct and when a parent post of a repeatable field group is deleted, its relationship connections with child repeatable field group items are deleted, but those child post items are not deleted.

We have an internal ticket to improve this in future releases, however, I don't have a time estimate to share at the moment.

For now, you can use a custom function attached to the "after_delete_post" hook ( ref: https://developer.wordpress.org/reference/hooks/after_delete_post/ ) which is executed every time a "story" post type is deleted. The function will cycle through all the available "bill-services" repeatable post items and delete the ones for which a parent "story" post doesn't exist:


add_action( 'after_delete_post', 'custom_services_cleanup_func', 1, 2 );
function custom_services_cleanup_func( $postid, $post ) {
	// exit if post that is getting deleted is not of type "story"
	if ( 'story' !== $post->post_type ) {
		return;
	}

	// get all repeating field post types 'bill-services'
	$args = array(
		'post_type'        => 'bill-services',
		'posts_per_page'   => -1,
		'post_status'      => 'publish',
	);
	$posts_array = get_posts( $args );

	foreach ($posts_array as $post) {
		// get the parent 'story' post
		$get_parent = toolset_get_related_posts( $post->ID, 'bill-services', 'child', 1, 0, array(), 'post_id', 'parent' );
		// if no parent exists, delete the current 'bill-services' post
		if(empty($get_parent)) {
			wp_delete_post( $post->ID, true);
		}
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2351429

My issue is resolved now. Thank you!