I have a custom post list in the admin panel. This list is paginated, so this is it's url :
wp-admin/edit.php?post_type=xxx
, if I open the default pagewp-admin/edit.php?post_type=xxx&paged=n
, if I go to the nth page of the list
Each time I delete permanently an item of that list I would like to be redirected to the same page of the list if this page still exists or to the previous page of the list if the same page is no longer relevant.
Let's say I have three pages for a total of 22 items where page one has 10 items, page two has 10 items and page three has 2 items.
if I delete the last item of page three, I should be redirected to page three (as there is still one item left there after deletion)
If I delete the last item of page three now, I should be redirected to page two (as now, page three doesn't exist anymore)
If I delete any item of page two, I should be redirected to page two (as page two still exists)
If I delete any item of page one, I should be redirected to page one (as page one still exists)
So far I'm just able to redirect to the default first page of the list like this:
function my_deleted_post_handler()
{
global $post;
$post_type = $post->post_type;
wp_redirect("http://www.xxxxxx.com/wp-admin/edit.php?post_type=".$post_type);
exit;
}
add_action('deleted_post','my_deleted_post_handler');
But I don't know how to get the page number I'm on after deletion (or I was on before deletion), so I can't redirect to the relevant page afterwards.
I tried to retrieve $_GET['paged']
, but the function above is triggered after the deletion when the "paged" variable is not part of the url anymore so it doesn't return any value.
How to achieve this?