Skip Navigation

[Resolved] trash or delete the post

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

Problem:

I want to add a front-end delete button for a post that has multiple relationships, and I need to understand the difference between “Trash the post” and “Delete the post,” as well as whether related relationships are also removed.

Solution:

“Trash the post” moves it to the WordPress Trash and allows recovery; “Delete the post” permanently removes it. In both cases, Toolset removes the relationships linked to the deleted post, but the related posts themselves are not deleted unless custom code using before_delete_post and toolset_get_related_posts() is implemented.

Relevant Documentation:

https://toolset.com/forums/topic/best-practice-when-deleting-posts-related-to-other-posts/

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.

This topic contains 1 reply, has 1 voice.

Last updated by Christopher Amirian 1 week, 6 days ago.

Assisted by: Christopher Amirian.

Author
Posts
#2810066

I have a post. with. multiple relationships with others.
I want to have a button on the front end to delete the post.

The options in the setup are "Action to perform > Trash the post" or "Action to perform > Delete the post".

What is the difference exactly ? I want to delete the relationships with this post at the same time of course.

#2810236

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support.

When configuring a front-end delete button using Toolset Forms, you have two primary options:

Trash the Post: This action moves the post to the WordPress Trash. The post remains in the database and can be restored later.

Delete the Post: This action permanently removes the post from the database.

Regarding relationships, when a post is deleted or trashed, Toolset automatically removes its associations with related posts. However, the related posts themselves remain unaffected.

As Toolset does not automatically delete related posts. If you want to delete related posts when a post is deleted, you'll need to implement custom code using WordPress hooks like before_delete_post.

You can use an example like this:

add_action('before_delete_post', 'delete_related_posts');
function delete_related_posts($post_id) {
    // Replace 'your_relationship' with your actual relationship slug
    $related_posts = toolset_get_related_posts($post_id, 'your_relationship', 'parent');
    foreach ($related_posts as $related_post_id) {
        wp_delete_post($related_post_id, true);
    }
}

This code retrieves all posts related to the post being deleted and then deletes them. Ensure you replace 'your_relationship' with the actual slug of your relationship.

Note: The code above is meant to give you guidance on how to use the hooks and is not a ready solution.

For more information:

https://toolset.com/forums/topic/best-practice-when-deleting-posts-related-to-other-posts/

Thanks.