I have many posts in my post custom post type and the “All Posts” is paged. When I click “Edit” link and want to edit some post, I want to auto redirect to real previous page I was.
For example I’m in the link https://example.com/wp-admin/edit.php?paged=18 and enter into edit one post. I want to redirect to mentioned link (page 18) after publish or edited post.
I tried a code but seems doesn’t work at all and it won’t redirect after publishing post or edit.
function wp_redirect_post_location( $location ) {
$prevurl = $_SERVER['HTTP_REFERER'];
if ( 'post' == get_post_type() ) {
if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) )
return $prevurl;
}
return $location;
}
add_filter( 'redirect_post_location', 'wp_redirect_post_location' );
And also tried this but didn’t worked:
add_action( 'wp_after_insert_post', 'redirect_after_save_post');
function get_redirect_url(){
$prevurl = $_SERVER['HTTP_REFERER'];
if (isset($_GET['post_type'])){
$page = $_GET['post_type'];
return $prevurl;
}
}
function redirect_after_save_post($post_id){
wp_redirect(get_redirect_url());
exit;
}
The second code redirect to https://example.com/wp-admin/post.php after publishing post, while I want to redirect to https://example.com/wp-admin/edit.php?paged=18 (Where I entered to post’s edit).
Is there any solution or any other code to do this what I want? Thanks