admin管理员组文章数量:1327691
The function wp_delete_post() will permanently delete when used on Custom Post Types. The force
flag only works on PAGE
and POST
cpt.
See this post as an explanation of how the force flag is being used: wp_delete_post() deletes post instead of moving it to trash
Question: A plugin I have is using this function, and I want to try hook in to wp_delete_post and move the item to trash instead of permanently deleting.
Is it possible to use before_delete_post
to try move things to the trash before it reaches the stage of permanently deleting?
Alternative: Don't have the item deleted at all. In my case I would be happy to update the post status or some meta key and then I'll run a permanent delete on those products with that meta key at a later date.
My attempt so far - this test is simply to try see if I can prevent the full wp_post_delete from running. This did not work, the post was permanently deleted still.
function delete_handler($postid){
return false;
}
add_action('before_delete_post', __NAMESPACE__.'\\delete_handler');
The function wp_delete_post() will permanently delete when used on Custom Post Types. The force
flag only works on PAGE
and POST
cpt.
See this post as an explanation of how the force flag is being used: wp_delete_post() deletes post instead of moving it to trash
Question: A plugin I have is using this function, and I want to try hook in to wp_delete_post and move the item to trash instead of permanently deleting.
Is it possible to use before_delete_post
to try move things to the trash before it reaches the stage of permanently deleting?
Alternative: Don't have the item deleted at all. In my case I would be happy to update the post status or some meta key and then I'll run a permanent delete on those products with that meta key at a later date.
My attempt so far - this test is simply to try see if I can prevent the full wp_post_delete from running. This did not work, the post was permanently deleted still.
function delete_handler($postid){
return false;
}
add_action('before_delete_post', __NAMESPACE__.'\\delete_handler');
Share
Improve this question
edited Feb 8, 2020 at 12:27
wharfdale
asked Feb 8, 2020 at 12:18
wharfdalewharfdale
3484 silver badges17 bronze badges
1 Answer
Reset to default 1Inside wp_delete_post()
there is the following code,
/**
* Filters whether a post deletion should take place.
*
* @since 4.4.0
*
* @param bool|null $delete Whether to go forward with deletion.
* @param WP_Post $post Post object.
* @param bool $force_delete Whether to bypass the trash.
*/
$check = apply_filters( 'pre_delete_post', null, $post, $force_delete );
if ( null !== $check ) {
return $check;
}
So I think with the following filtering you might be able to prevent the deletion.
function prevent_cpt_delete( $delete, $post, $force_delete ) {
// Is it my post type someone is trying to delete?
if ( 'my_post_type' === $post->post_type && ! $force_delete ) {
// Try to trash the cpt instead
return wp_trash_post( $post->ID ); // returns (WP_Post|false|null) Post data on success, false or null on failure.
}
return $delete;
}
add_filter('pre_delete_post', 'prevent_cpt_delete', 10, 3);
本文标签: hooksOverride wpdeletepost and move to trash instead for custom post type
版权声明:本文标题:hooks - Override wp_delete_post and move to trash instead for custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742216565a2434674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论