I'm trying to redirect a user to a specific post after logging in. This post is the latest post of a custom post type where the author is the current logged in user.
I currently have to following code to get the specific post url:
function member_permalink () {
$user_id = get_current_user_id();
$args=array(
'post_type' => 'cursist',
'author' => $user_id
);
$current_user_posts = get_posts( $args );
$post_link = get_permalink( $current_user_posts->ID );
return $post_link;
}
And the code to redirect the user after login (taken from the Codex)
function my_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
//check for subscribers
if (in_array('subscriber', $user->roles)) {
// redirect them to another URL, in this case, the homepage
$redirect_to = member_permalink();
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
However I get redirected to WP-admin instead of the post.
If I do a var_dump of the function to get the specific post url I get the following in return (which is the data that I want):
string(52) "http://example.com/cpt-slug/niels-pilon/"
I'm probably doing something wrong but can't figure out what.
$redirect_to
tohome_url()
. The author of the CPT has also a role as member as I create the CPT with a function when registering a new user.