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 )
Languages: English (English )Chinese (Simplified) (简体中文 )
Timezone: Asia/Hong_Kong (GMT+08:00)
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.
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();
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.
Languages: English (English )Chinese (Simplified) (简体中文 )
Timezone: Asia/Hong_Kong (GMT+08:00)
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.
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']
Languages: English (English )Chinese (Simplified) (简体中文 )
Timezone: Asia/Hong_Kong (GMT+08:00)
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.