You should pass the $user
as a parameter to member_permalink()
instead of relying on get_current_user_id()
.
From the codex about the login_redirect
filter:
The $current_user global may not be available at the time this filter
is run. So you should use the $user global or the $user parameter
passed to this filter.
Sample updated code:
function member_permalink( $user = null ) {
if ( null == $user || is_wp_error( $user ) ) {
$user_id = get_current_user_id();
} else {
$user_id = $user->ID;
}
$args = array(
'post_type' => 'cursist',
'author' => $user_id,
'numberposts' => 1,
);
$current_user_posts = get_posts( $args );
if ( empty( $current_user_posts ) ) {
return;
}
$post_link = get_permalink( $current_user_posts[0] );
return $post_link;
}
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
$redirect_to = member_permalink( $user );
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
[edit]: I made the $user
parameter optional to provide flexibility in case you use this function without parameters in some other places.