Skip Navigation

[Escalated to 2nd Tier] Does toolset_before_association_delete get raised when a post deleted

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 12 replies, has 2 voices.

Last updated by Christian Cox 3 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1880913

I have a one to one relationship setup for two post types - I'm catching action toolset_before_association_delete in my code
If I disconnect the posts, then the relationship is deleted and the action is raised correctly.
However if one of the posts is deleted, the relationship is deleted, but the action is not raised at all.

This does not seems correct to me as in both cases the relationship is deleted. Is this a bug? Can it be fixed for consistency?

#1882023

Hi, I ran some tests and the behavior you described seems to be consistent for all relationship types, not just one-to-one, but I'm not quite sure if it's expected or a bug. The documentation isn't specific about the case where a post is deleted without being first disconnected from a relationship. I have asked my 2nd tier team for some feedback about the expectation here, and I'll let you know what I find out. In the meantime I suppose a workaround would be to hook into the before_delete_post event for these post types and query for related posts using the post relationships API.

#1882761

Quick update - my 2nd tier team discussed this with our developers and we are in agreement that the toolset_before_association_delete hook should be triggered when one of the related posts is deleted. We are escalating this to the developers for resolution, but I don't have a timeline available for the delivery of this update just yet. Instead, a workaround with before_delete_post or delete_post and the Post Relationships APIs might be your best bet. Let me know if you have questions about implementing those post relationships APIs and I can give you guidance.

#1882965

Many thanks Christian - it's not a job stopper for me.

#1889329

Hey Christian, this has popped back onto our critical path as we don't have an alternative hook we can use - delete or before_delete don't work for us. We can think of some complex alternatives, but clearly a fix would be a good solution.

Any idea when we could get a patch for this or perhaps event some pointers so that we could modify the plugin core and raise the action ourselves?

Thanks,

#1889661

Our developers haven't had a chance to look into this yet, so I don't have a patch available. If one becomes available I'll let you know here. Until then, I don't really have any information about patching core plugin files. I might be able to think of an alternative if you explain why the delete post and before delete post hooks don't work in this case. Are you deleting related posts from the backend, or from the front-end using Forms?

#1890449

Hey Christian. I'm synchronizing content between two sites using REST API. The main site raises post insert, delete and update requests to 2nd site. On REST insert of posts, I generate child posts and create toolset relationships to them. When my main site POSTS a delete over REST, the parent post is deleted on the 2nd site, this action deletes the relationships to the child posts, but I have no mechanism to delete the child posts themselves. All deletes are forced deletes, so there is no before_delete action raised, the only action I have available is delete_post which is raised after the meta data for the post is deleted and, I guess, the Toolset relationships. delete_post gives me the post object of the deleted post, but no information that allows me to access and delete former child posts. The whole process is transparent to admin and site users.

#1890863

Hmm okay I see, it's a bit of a more complex scenario. I assume there is no way to modify the process by which the main site triggers deleting the parent post? If there were such a way, then it would be possible to query related posts before deleting the parent, but it sounds like you are saying there is no easy way to modify this deletion process to trigger code before calling wp_delete_post.

And there is no way to execute code between deleting the parent post on the main site and triggering the DELETE command on the synched site? If there is a way to trigger code between those steps, then I suppose you could set up a REST endpoint on the synched site that queries related posts of that parent post, and returns the child post ID(s). Then on the main site you could delete those corresponding child posts, trigger DELETES for those child posts back to the synched site, then trigger DELETE to delete the parent post on the synched site.

Without knowledge of the main site's post management processes, I'm not sure what is possible here.

#1891349

I think you answered the question. Yes, there are alternative approaches - but all of them are complex. I would prefer to spend the time patching the Toolset code myself to raise the relationship deleted action rather than building an alternative approach to something that should just work.

#1891647

Okay understood. As a policy, supporters must discourage you from advising core plugin modifications without developer input and confirmation. We are not trained to work on the core codebase, and don't have the information available to make reliable recommendations for those kinds of changes. We're likely to break something unexpectedly or make the situation worse. If the developers make a patch available before a stable release, I'll update you here. In my opinion, you could probably create a reliable workaround faster than our developers will have a chance to get to this and produce a patch given the current schedule and development request pipeline. If you'd prefer to adjust the core plugin files instead, unfortunately I can't offer any advice or support. We recommend you do not make modifications to core plugin files.

#1893871

After some discussion with the team, our developers have explained that you should be able to delete child posts as shown here when working with the delete_post hook:

add_action( 'delete_post', 'ts_delete_children', 1, 2 );
function ts_delete_children( $post_id, $post ){

    $parent = 'project'; // Edit parent post type slug

    $relationship = 'project-task'; // Edit relationship slug

    // do not edit below this line

    if ( $post->post_type == $parent )
    {
        //get child tasks
        $children = toolset_get_related_posts( $post_id, $relationship, array('query_by_role' => 'parent','role_to_return' => 'child') );

        if ( is_array( $children ) )
        {
            foreach ($children as  $child) 
            {
                wp_delete_post( $child );
            }
        }
    }
}

This code is intended to work with post relationships created with Types 3+.

#1926899

Hey Christian - i have tried to use the delete hook as suggested above. But as expected, and the WP docs suggest, the post, post meta and it would seem toolset relationships are all unavailable after the delete. So this does not help.

Any update as to when this might become available? nothing in the changelog for the last release of types so I guess it wasn't it it.

Thanks,

#1927089

I apologize, I must admit I did not actually test the code because I assumed the developers had. However, in a few local tests, I can confirm that the code does not seem to work as expected. I'm asking my 2nd tier team for some additional feedback here and will let you know what I find out.