Redirect after login to newest post

Greetings,

We publish a monthly report which is protected by S2. We currently have it set-up to redirect to a basic post listing page after logging in. The user finds the most current publication link (usually at the top of the list) and proceeds from there. Is there anyway that after a user logs in that they would be redirected to the most current publication? I am not seeing any way to do this except to manually change the redirect URL after each publication.

Thank you in advance for any support.

Try this: https://wordpress.org/plugins/eth-redirect-to-latest-post/

Or you could add this as an mu-plugin:

<?php
function s2_lwp_redirect_to_latest_post() {
	if ( !is_page( S2MEMBER_LOGIN_WELCOME_PAGE_ID ) ) {
		return; // nothing to do if no initial attempt to go to Login Welcome Page
	}

	if ( current_user_is( 's2member_level1' ) ) { // applies to level 1 only
		$args = array(
		'posts_per_page' => '1',
		'post_type' => 'post'
		);
		$post = get_posts( $args );
		if( $post ) {
			$url = get_permalink( $post[0]->ID );
			wp_redirect( $url, 301 ); 
			exit;
		}
	}
}
add_action( 'template_redirect', 's2_lwp_redirect_to_latest_post' );

I am not sure how to configure this plugin. I install it and then what? lol Do I need to make an adjustment in the S2 for the redirect after login?

Go to Settings -> Permalinks and change the slug shown there (at the bottom of the page) to the slug for your Login Welcome Page.

OK, this seems to be working. THANKS YOU! Now can it be the latest post of a certain category?

Just change the args bit to this:

                $args = array(
			'posts_per_page' => '1',
			'post_type' => 'post',
			'category' => '13'
		);

where the number represents the category ID.

I am using the plugin and haven’t adjusted any code yet. Which file holds the script you are referring to?

The plugin is just for doing what you originally asked. If you want to sort by category too, then my code is more suitable.

OK, i went ahead and create mu-plugin. However, when I put the code in place it caused the site to white-screen.

here is my code (the editor did tell me there was an error for some reason with the category line that we added) :

<?php function s2_lwp_redirect_to_latest_post() { if ( !is_page( S2MEMBER_LOGIN_WELCOME_PAGE_ID ) ) { return; // nothing to do if no initial attempt to go to Login Welcome Page } if ( current_user_is( 's2member_level1' ) ) { // applies to level 1 only $args = array( 'posts_per_page' => '1', 'post_type' => 'post' 'category' => '8' ); $post = get_posts( $args ); if( $post ) { $url = get_permalink( $post[0]->ID ); wp_redirect( $url, 301 ); exit; } } } add_action( 'template_redirect', 's2_lwp_redirect_to_latest_post' ); ?>

You have added a closing tag (i.e. ?>) at the end. NEVER DO THAT. I omitted it for a reason.

I am guessing you have whitespace either after that tag or before the opening tag.

Get rid of the closing tag, and check that there’s no whitespace before the opening tag.

I added the closing tag because I thought that would fix it. There are no whitespaces.I am showing an error at line 11 (category). Could that be the issue?

<?php function s2_lwp_redirect_to_latest_post() { if ( !is_page( S2MEMBER_LOGIN_WELCOME_PAGE_ID ) ) { return; // nothing to do if no initial attempt to go to Login Welcome Page } if ( current_user_is( 's2member_level1' ) ) { // applies to level 1 only $args = array( 'posts_per_page' => '1', 'post_type' => 'post' **'category' => '8'** ); $post = get_posts( $args ); if( $post ) { $url = get_permalink( $post[0]->ID ); wp_redirect( $url, 301 ); exit; } } } add_action( 'template_redirect', 's2_lwp_redirect_to_latest_post' );

This is the code attached. Is this correct?

The code is correct (without the asterisks of course) but I am guessing that there is no category with an ID of 8 on your site. How have you come up with that ID?

I suggest using Reveal IDs to get the correct ID.

You miss a coma at the end of the line above the one shown in the error message. Common PHP trick to full the developers… :slight_smile: