Hello i'm trying to use function toolset_get_related_post with wordpress hook before_delete_post
I have toolset CPT "fattura", toolset repeteable fieds "spese-ant-grup"
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
What is the link to your site?
Local site, not public
I tried this code (it is a test code):
add_action('before_delete_post' ,'aggiorna_somma_spese_anticipate_in_fattura_delete', 20,1);
function aggiorna_somma_spese_anticipate_in_fattura_delete($post_id) {
if ( get_post_type( $post_id ) == 'spese-ant-grup' ){
$idfattura = get_the_ID();
$genitore_fattura = toolset_get_related_post( // Get ID of parent cliente page
$idfattura,
'spese-ant-grup', //slug of relationship
'parent'
);
$fattura_spesa_anticipata_ora = 0;
update_post_meta($idfattura, 'wpcf-spese-anticipate-totale', $fattura_spesa_anticipata_ora);
}}
Deleting a post (spese-ant-grup) from toolset views (with form/cred delete button) my page go to loop and don't refresh, i made various test and problem seem to be with "toolset_get_related_post" with "before_delete_post" together.
I'm missing something? Can i have some simple working example from you or To know the most effective method ?
I can use correctly in other functions "toolset_get_related_post" with cred_save_data hook.
I use wordpress before_delete_post because don't exist some cred delete hook (At least as far as I know )
Hello,
Toolset repeatable field groups are based on one-to-many relationship, so each item of "spese-ant-grup" field group is actually a post, and one "fattura" can connect to multiple instance "spese-ant-grup" posts.
In your case, you need to try another function toolset_get_related_posts(), see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Query related posts by a set of conditions.
And there is an example code in above document, you can click link "Usage examples" and find the example code.
Hello Luo and thanks for help, i tried this simple function and result is the same, deleting a post "spese-ant-grup" from views my page is locked and i have to refressh manually:
add_action('before_delete_post' ,'aggiorna_somma_spese_anticipate_in_fattura_delete', 2,1);
function aggiorna_somma_spese_anticipate_in_fattura_delete($post_id) {
$idfattura = get_the_ID();
if ( get_post_type($post_id) == 'spese-ant-grup' ){
$books = toolset_get_related_posts(
$idfattura,
array( 'fattura', 'spese-ant-grup' ),
'child',
100, 1,
array(),
'post_object'
);
$fattura_spesa_anticipata_ora = 0;
update_post_meta($idfattura, 'wpcf-spese-anticipate-totale', $fattura_spesa_anticipata_ora);
}}
I would like to have from you a simple working code that include together wordpress hook 'before_delete_post' and function toolset_get_related_posts, the i can adapt code for myself.
Thanks again and sorry for bothering
I assume we are talking about this case:
1) Two post types:
- fattura (with a custom field "spese-anticipate-totale")
- spese-ant-grup
With one-to-many relationship "fattura-spese-ant-grup" between post types "fattura" and "spese-ant-grup"
When user delete/trash a spese-ant-grup post, you are going to update the field "spese-anticipate-totale" value of parent "fattura" post.
If it is, please try these:
1) in single "fattura" post, setup a post view:
- query "spese-ant-grup" posts
- filter by post type relationship:
Select posts in a fatturas spese-ant-grups relationship that are related to the Post where this View is shown.
- in "Loop Editor", display the delete post link, like this:
[cred_delete_post_link action='trash' text='Delete %TITLE%' message='Are you sure you want to delete this post?' message_show='1' class='cred-refresh-after-delete']
See screenshot delete-link.JPG, this will be able to trash post after click the delete post.
2) Modify your PHP codes as below:
add_action('wp_trash_post' ,'aggiorna_somma_spese_anticipate_in_fattura_delete', 20,1);
function aggiorna_somma_spese_anticipate_in_fattura_delete($post_id) {
if ( get_post_type( $post_id ) == 'spese-ant-grup' ){
$fattura_id = toolset_get_related_post( // Get ID of parent fattura post
$post_id,
'fattura-spese-ant-grup', //Slug of the relationship
'parent'
);
$fattura_spesa_anticipata_ora = 0;
update_post_meta($fattura_id, 'wpcf-spese-anticipate-totale', $fattura_spesa_anticipata_ora);
}
}
Please replace the "fattura-spese-ant-grup" with the relationship slug, if "spese-ant-grup" is a repeatable field group, then you can use the "spese-ant-grup" directly.
More help:
https://developer.wordpress.org/reference/hooks/wp_trash_post/
Fires before a post is sent to the trash.
Thanks Luo, your code is working good (on trashing the post).
But how can i trigger the hook deleting the post (not trashing)?
I already edited my views:
[cred_delete_post_link action='delete' text='Delete %TITLE%' message='Are you sure you want to delete this post?' message_show='1' class='cred-refresh-after-delete']
Yes, it is possible to trigger the hook "before_delete_post" by deleting the post (not trashing).
You can setup the parameter "priority" to 1 and test again, for example:
add_action('before_delete_post' ,'aggiorna_somma_spese_anticipate_in_fattura_delete', 1,1);
function aggiorna_somma_spese_anticipate_in_fattura_delete($post_id) {
if ( get_post_type( $post_id ) == 'spese-ant-grup' ){
$fattura_id = toolset_get_related_post( // Get ID of parent fattura post
$post_id,
'fattura-spese-ant-grup', //Slug of the relationship
'parent'
);
$fattura_spesa_anticipata_ora = 0;
update_post_meta($fattura_id, 'wpcf-spese-anticipate-totale', $fattura_spesa_anticipata_ora);
}
}
https://developer.wordpress.org/reference/functions/add_action/
$priority
(int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
Thanks Luo it worked perfectly! My issue is resolved now.