Last active
October 12, 2020 21:56
-
-
Save rochow/7377e1410c4163720e9f to your computer and use it in GitHub Desktop.
WordPress - Redirect Archive to Most Recent Post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // In this example if trying to access /work/ it'll 302 redirect to the most recent post instead | |
| add_action('template_redirect','redirect_to_latest'); | |
| function redirect_to_latest() { | |
| if( is_post_type_archive('work') ) { | |
| $latest = get_posts( 'post_type=work&posts_per_page=1' ); | |
| if($latest) { | |
| wp_redirect( get_permalink( $latest[0]->ID ) ); | |
| exit; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment