Skip to content

Instantly share code, notes, and snippets.

@rochow
Last active October 12, 2020 21:56
Show Gist options
  • Select an option

  • Save rochow/7377e1410c4163720e9f to your computer and use it in GitHub Desktop.

Select an option

Save rochow/7377e1410c4163720e9f to your computer and use it in GitHub Desktop.
WordPress - Redirect Archive to Most Recent Post
<?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