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 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)
This topic contains 4 replies, has 2 voices.
Last updated by 3 years, 10 months ago.
Assisted by: Jamal.