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?
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
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?
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.
This worked. Thanks so much!
My issue is resolved now. Thank you!