Skip Navigation

[Resolved] A post that searches using a RFG cannot be deleted without deleting RFG data

This thread is resolved. Here is a description of the problem and solution.

Problem:

Trash child posts when parent post is set to draft status.

Solution:

You can try with "transition_post_status" action hook, for example:

https://toolset.com/forums/topic/a-post-that-searches-using-a-rfg-cannot-be-deleted-without-deleting-rfg-data/#post-1664369

Relevant Documentation:

https://developer.wordpress.org/reference/hooks/transition_post_status/

This support ticket is created 4 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/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by ceciliaW9184 4 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1658551

This is a continuation of Ticket: 1658525

Sorry I was out for the past 2 weeks. I added a comment to that ticket.

Your first snippet of code worked on trash Posts. Thank you!

Can you add a hook that will do the same to Expired or Draft posts?

#1659675

Hello,

For the changing a post from published status to Draft status, you can try these:

add_action( 'transition_post_status', 'draft_function', 10, 3 );
function draft_function( $new, $old, $post ) {
    if ( ( $new == 'draft' ) && ( $old == 'publish' ) && ( $post->post_type == 'your_post_type' ) ) {
        func_delete_child_related_posts( $post->ID )
    }
}

Please replace "your_post_type" with your custom post type slug.

More help:
https://developer.wordpress.org/reference/hooks/transition_post_status/

For the Expired posts, how do you setup the post is expired, you can change the post status from published to draft when it is expired, then it will do the same thing as draft post

#1663351

I tried both of these snippets of code to put it into draft status and they didn't work:

SNIPPET #1:

<?php
/**
* Update draft trials that use RFG Locations.
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_action( 'transition_post_status', 'draft_function', 10, 3 );
function draft_function( $new, $old, $post ) {
if ( ( $new == 'draft' ) && ( $old == 'publish' ) && ( $post->post_type == 'paid-clinical-trial' ) ) {

$children = toolset_get_related_posts(
$post_id,
'additional-locations',
array(
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_id',
'role_to_return' => 'child'
)
);

foreach ($children as $key => $child) {

transition_post_status($child);
}
}
}

SNIPPET #2:

add_action( 'transition_post_status', 'draft_function', 10, 3 );
function draft_function( $new, $old, $post ) {
if ( ( $new == 'draft' ) && ( $old == 'publish' ) && ( $post->post_type == 'paid-clinical-trial' ) ) {
func_delete_child_related_posts( $post->ID )
}
}

What did i do wrong?

#1664369

Please try these:
1) Deactivate Custom code snippet you mentioned above
The action hook transition_post_status won't work in Custom code snippet

2) Put below codes in your theme file functions.php:

add_action( 'transition_post_status', 'draft_function', 10, 3 );
function draft_function( $new, $old, $post ) {
	if ( ( $new == 'draft' ) && ( $old == 'publish' ) && ( $post->post_type == 'paid-clinical-trial' ) ) {

		$children = toolset_get_related_posts(
			$post->ID,
			'additional-locations',
			array(
				'query_by_role' => 'parent',
				'limit' => 999,
				'return' => 'post_id',
				'role_to_return' => 'child'
			)
		);

		foreach ($children as $key => $child) {
			wp_trash_post($child);
		}
	}
}

Replace "additional-locations" with your post type relationship slug, and test again. It works fine in my localhost.

#1664777

This worked. Thanks so much!

#1664779

My issue is resolved now. Thank you!