Skip Navigation

[Resolved] Redirect user to own post after login

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 1 voice.

Last updated by Minesh 4 weeks, 1 day ago.

Assisted by: Minesh.

Author
Posts
#2789843

This site has a custom post type for members. Each member has a profile page and has a user account so they can edit their own information. Now, the client wants a user (member) to be redirected to their own profile page (which is the only post they can manage) after login.

#2789997

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Is the user profile page is mangaged as custom post type and has a one post per user?

If yes:

There are many options and you can select any one of those:
- hidden link

You can use any of the following plugin that may help you:
- https://wordpress.org/plugins/peters-login-redirect/
- https://wordpress.org/plugins/wp-after-login-redirect-advanced/

#2790057

Hi Minesh,

Let me try to explain it another way.

- There's a custom post type called "members".
- Each member has his own WordPress user account.
- Each member is set as the author for his own member post.
- Each user can only edit his own member info.

What we want is that when the user logs in, he will be redirected to his own member post.

I know I can use redirect plugins, but how can I redirect the user to his own member post instead of a static URL? It's not like there's a /my-account URL that I can redirect to, since each member post has a unique URL (site.nl/member/[member-name]).

#2790058

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - for that, you can use the WordPress native filter "login_redirect" where we can try to find the member post where author of member post will be current loggedin useer and if found we will try to redirect user to that found member post.

Please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_filter("login_redirect", "func_custom_login_redirect", 10, 3);
function func_custom_login_redirect( $redirect_to, $request, $user ) {
            
          $member_post = get_posts( array(
                'post_type' => 'member',
                'numberposts' => 1,
                'author' => get_current_user_id()
            ) );
        
        if( $member_post  && count($member_post ) > 0 ){
            wp_redirect(get_permalink($member_post[0]->ID)); 
            exit;
        }
        
   
    return $redirect_to;
}

You can adjust the code as required.