Skip Navigation

[Resolved] wp_trash_post is not working with [cred-delete-post]

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

Last updated by Luo Yang 3 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2130437

Hi, I've a Repeatable Group 'fishes' in my custom fields.

The user can add 'fishes' by a Toolset form and he also can delete then with [cred-delete-post action='trash' onsuccess='self'].

I've added a hook like this one to [cred-delete-post]:


// Generic code for debug output
function log_me($message) {
  if (WP_DEBUG === true) {
    if (is_array($message) || is_object($message)) {
      error_log(print_r($message, true));
    } else {
      error_log($message);
    }
  }
}


add_action( 'wp_trash_post', 'fishes_cred_delete_post' );
function fishes_cred_delete_post( $post_id ) {
  log_me('----------DELETE--'.$post_id.'------');
  

  if ( $deleted_post->post_type == 'fishes' ) {
  log_me('----------fishes---------');
  ....
  }
}

It was working fine, but after the latest Toolset update it stopped working. I've activated WP Debug and but in debug.log I don't get any output of the log_me funtion when I delete a 'fishes'.

It seems the action is not triggered.

Can you help me?

#2130665

Hi, I tried 2 different approaches, but it still hasn't worked.

I tied with cred_redirect_after_delete_action:

add_filter( "cred_redirect_after_delete_action", "prefix_custom_redirect", 10, 2 );
function prefix_custom_redirect( $url, $post_id ) { 
  
  log_me('----------DELETE redirect --'.$post_id.'------');
  $deleted_post = get_post( $post_id );

  if ( $deleted_post->post_type == 'fishes' ) {
      log_me('----------fishes---------');
  }
    return $url;
}

It just does nothing, I also don't get any output of log_me() in debug.log

Then I created an Edit form with the submit button and tried this custom code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
  log_me('----------TRASH--'.$post_id.'------');

    if ($form_data['id']==1039)
    {
        $my_post = array(
            'ID'           => $post_id,
            'post_status'   => 'trash',
        );

        wp_update_post( $my_post );
    }
}

It changes the status of the post, but it doesn't remove it from the Repeatable Group and I got this error in debug.log:

[31-Jul-2021 18:58:20 UTC] PHP Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246
Stack trace:
#0 /public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, 'parent')
#1 /public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/public_api/m2m.php(110): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(0, 'fishes', Array)
#2 /public_html/wp-content/plugins/cred-frontend-editor/application/models/form/post.php(671): toolse in /public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 246

One last thing, I missed this line, before the conditional IF, in the custom code of the previous message:

$deleted_post = get_post( $post_id );

cheers

#2131411

Hi,

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

Your observation is correct and in Toolset Forms 2.6.10, some changes were introduced to how the "cred-delete-post" shortcode works with the repeatable field group posts.
( https://toolset.com/download/toolset-cred/#changelog )

Since these post types don't offer the standard post-management user interface, it makes sense never to send them to trash and always delete them when this "cred-delete-post" shortcode is used.

This is the reason why the "wp_trash_post" hook is not firing in this case and you can replace it with the "delete_post" hook, instead.
( ref: https://developer.wordpress.org/reference/hooks/delete_post/ )

regards,
Waqar

#2134663

Hi Waqar, thank you for the suggestion. I setup the "delete_post" hook, now it works, I get the output in debug.log.

My problem now is that I need to get the parent post ID and the value of a custom field of the deleted Repeatable Group post.

I tried this:

$team = toolset_get_related_post( $post_id, 'fishes' );
$fishmeasure = get_post_meta( $post_id, 'wpcf-fishmeasure', true );

But I get in debug.log zero and empty value:

$team: 0
$fishmeasure:

How can I get those values?

cheers

#2135379

Hello,

Waqar is on vacation, I assume the original question of this thread is resolved, for other new questions, please check the new thread here:
https://toolset.com/forums/topic/i-need-to-get-the-parent-post-id-and-the-value-of-a-custom-field-of-the-deleted-repeatable-group-post/