Skip Navigation

[Resolved] Best practice when deleting posts related to other posts

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

Problem:
The user would like to prevent deletion of a post if it has related posts to it.

Solution:
There no built-in way to do this. But, I can imagine at least two ways.

- Simple way(if users interact with the system only from the frontend). Wrap the delete link of a post inside a conditional shortcode. The conditions should check if the post has any related posts in different relationships.
- Custom code way(should work for frontend and backend). Create a hook on the before_delete_post action, and check if the post has related posts. If it does, redirect to a different page to prevent Toolset from deleting the post. Check this StackOverflow thread https://wordpress.stackexchange.com/questions/92155/how-to-prevent-a-post-from-being-deleted
You can use Toolset relationships functions to check for the existence of related posts:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
- https://developer.wordpress.org/reference/hooks/before_delete_post/

We have published a snippet that actually deletes the related posts when a post is deleted, it may inspire you if you want to implement the custom code solution https://kb.onthegosystems.com/code-snippet/automatically-delete-child-posts-when-parent-is-deleted/

This support ticket is created 3 years, 10 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.

Our next available supporter will start replying to tickets in about 2.01 hours from now. Thank you for your understanding.

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: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by himanshuS 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1897725

I have 5 CPTs and many of them are related to one another.

What happens I use cred-delete-post to delete a post that is related to other posts? Does the system allow it without impacting other posts? Or will the system only delete one post and keep remaining posts valid but remove the relationship?

I don't see a warning message when I click on delete post button. Is there a way to add a message?

Also, is there a way to not delete the post if its related to other published or draft posts?

I am pulling lot of data from parent or child posts to populate my templates. I want to sure that data is only removed if 1) user knows the implications on related post types and templates , 2) No data is corrupted in related post types because of the deletion.

#1898697

Hello and thank you for contacting the Toolset support.

What happens I use cred-delete-post to delete a post that is related to other posts?
Toolset will delete or trash the post, depending on the attribute "action", and it will delete the associations to other posts. The related posts will not be deleted or trashed.

I don't see a warning message when I click on delete post button. Is there a way to add a message?
No, there's no confirmation dialog for this shortcode but you can set the action to "trash" instead of "delete", which would give the ability to undo the action in wp-admin if needed, or through a view that display posts from the trash.

is there a way to not delete the post if its related to other published or draft posts?
I am not sure to understand this question. But as I said above, you can set the action to "trash" so the post is not completely deleted from the database.

I hope this answers your request. Let me know if you have any further questions.

#1898913

Jamal,

My last questions: is there a way to not delete the post if its related to other published or draft posts?

What I meant was, is there a way to avoid deletion of a post if it has an association with another post? My posts are interlinked and data is transferred from one to another. I don't want the user to delete a post when there is an association that feeds into other posts.

Ideal scenario: user should be able to delete posts if there is no association with other posts.

#1899289

is there a way to not delete the post if its related to other published or draft posts?
There no built-in way to do this. But, I can imagine at least two ways.

- Simple way(if users interact with the system only from the frontend). Wrap the delete link of a post inside a conditional shortcode. The conditions should check if the post has any related posts in different relationships.
- Custom code way(should work for frontend and backend). Create a hook on the before_delete_post action, and check if the post has related posts. If it does, redirect to a different page to prevent Toolset from deleting the post. Check this StackOverflow thread https://wordpress.stackexchange.com/questions/92155/how-to-prevent-a-post-from-being-deleted
You can use Toolset relationships functions to check for the existence of related posts:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
- https://developer.wordpress.org/reference/hooks/before_delete_post/

We have published a snippet that actually deletes the related posts when a post is deleted, it may inspire you if you want to implement the custom code solution hidden link

#1899575

Thanks Jamal. My issue is resolved.