Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jayontraps/a2b5e4f2b9266489bdfa to your computer and use it in GitHub Desktop.
Save jayontraps/a2b5e4f2b9266489bdfa to your computer and use it in GitHub Desktop.
Wordpress wp: get custom post type archive listing by year
// example 'news' cpt
function myquery_custom_post_rewrite( $rewrite_rules ) {
$pagination_base = $GLOBALS['wp_rewrite']->pagination_base;
$myqueryslug = 'news';
$year_archive = array(
$myqueryslug . '/([0-9]{4})/' . $pagination_base . '/?([0-9]{1,})/?$' => 'index.php?post_type=' . $myqueryslug . '&year=$matches[1]&paged=$matches[2]',
$myqueryslug . '/([0-9]{4})/?$' => 'index.php?post_type=' . $myqueryslug . '&year=$matches[1]',
);
$rewrite_rules = $year_archive + $rewrite_rules;
return $rewrite_rules;
}
add_filter('rewrite_rules_array', 'myquery_custom_post_rewrite');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment