您可以使用$\\u SERVER[\'HTTP\\u REFERER\']获取用户来自的URL。
下面的代码将找到前一个URL的paged GET变量,以确定用户所在的页面,然后计算该特定帖子类型(仅已发布)剩下的帖子数量,以及显示所有帖子所需的页面数量,然后采取适当的重定向操作。
function my_deleted_post_handler()
{
global $post;
// URL the user came from
$referer = $_SERVER[\'HTTP_REFERER\'];
// the page the user was on
$page = preg_match(\'/http:\\/\\/.*&paged=(\\d*)/\', $referer, $matches);
$page_num = $matches[1];
// how many published posts of $post_type?
$post_type = $post->post_type;
$num_posts = wp_count_posts($post_type)->published;
// how many pages of 10 items?
$pages = ceil($num_posts / 10);
if($pages < $page_num) {
// the page the user was on doesn\'t exist anymore
wp_redirect(\'http://www.xxxxxx.com/wp-admin/edit.php?post_type=\' . $post_type . \'&paged=\' . $page_num - 1);
} else {
// the page still exists
wp_redirect(\'http://www.xxxxxx.com/wp-admin/edit.php?post_type=\' . $post_type . \'&paged=\' . $page_num);
}
exit();
}
add_action(\'deleted_post\',\'my_deleted_post_handler\');