好吧,有趣的问题。就我所研究的而言,在删除帖子时调用钩子将删除的帖子ID作为参数传递。在删除父页时,它工作得很好,但当您挂接一个函数时trashed_post
若要删除子页面,它将无法工作,因为每次它都会获得垃圾帖子id,这里它将是最近删除的子页面的id。所以它不会像预期的那样工作。
现在来看解决方案。在这里,我用一个SQL命令重写了您的函数,将子页面的post状态更改为trash
-
function delete_post(){
global $post;
$deletepostlink= add_query_arg( \'frontend\', \'true\', get_delete_post_link( get_the_ID() ) );
if (current_user_can(\'edit_post\', $post->ID)) {
echo \'<span><a class="post-delete-link" onclick="return confirm(\\\'Are you sure to delete?\\\')" href="\' . $deletepostlink . \'">Delete this </a></span>\';
}
}
//Redirect after delete post in frontend
add_action(\'trashed_post\',\'trash_redirection_frontend\');
function trash_redirection_frontend($post_id) {
if ( filter_input( INPUT_GET, \'frontend\', FILTER_VALIDATE_BOOLEAN ) ) {
$args = array(
\'posts_per_page\' => -1,
\'order\'=> \'ASC\',
\'post_parent\' => $post_id,
\'post_type\' => \'page\'
);
// Filter through all pages and find Portfolio\'s children
$children = get_children( $args );
global $wpdb;
foreach($children as $child){
$childs[$child->ID] = $child->ID;
}
$sql = "UPDATE {$wpdb->posts} SET post_status = \'trash\' WHERE ID IN (" . implode( \', \', $childs ) . ")";
$wpdb->query($sql);
wp_redirect( get_option(\'siteurl\').\'/\' );
exit;
}
}
希望对你有帮助。