Change the Archive Link Structure And Add Rewrites For Custom Post Type In WordPress

In this blog, we will learn about how to change the URL structure and add rewrites for Custom Post Type Archive Pages

Let’s add re-writes,

<?php
/**
 * Date Archive Redirect For CPT.
 *
 */

namespace YourNameSpace;

add_action( 'init',  function() {
	/**
	 * Day Archive Rewrite Rule
	 *
	 * Expected URL Structure = /{post-type}/{year}/{month}/{date}/
	 *
	 * post-type-slug - archive_post_type
	 * ([0-9]{4}) - date_archive_year 2021
	 * ([0-9]{1,2}) - date_archive_month 11
	 * ([0-9]{1,2}) - date_archive_day 01
	 *
	 */
	add_rewrite_rule(
		'post-type-slug/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$',
		'index.php?archive_post_type=post-type-slug&date_archive_year=$matches[1]&date_archive_month=$matches[2]&date_archive_day=$matches[3]',
		'top'
	);

	/**
	 * Month Archive Rewrite Rule
	 *
	 * Expected URL Structure = /{post-type}/{year}/{month}/
	 *
	 * post-type-slug - archive_post_type
	 * ([0-9]{4}) - date_archive_year 2021
	 * ([0-9]{1,2}) - date_archive_month 11
	 *
	 */
	add_rewrite_rule(
		'post-type-slug/([0-9]{4})/([0-9]{1,2})/?$',
		'index.php?archive_post_type=post-type-slug&date_archive_year=$matches[1]&date_archive_month=$matches[2]',
		'top'
	);

	/**
	 * Year Archive Rewrite Rule
	 *
	 * Expected URL Structure = /{post-type}/{year}/
	 *
	 * post-type-slug - archive_post_type
	 * ([0-9]{4}) - date_archive_year 2021
	 *
	 */
	add_rewrite_rule(
		'post-type-slug/([0-9]{4})/?$',
		'index.php?archive_post_type=post-type-slug&date_archive_year=$matches[1]',
		'top'
	);
} );

add_filter( 'query_vars', function( $query_vars ) {
	$query_vars[] = 'archive_post_type';
	$query_vars[] = 'date_archive_year';
	$query_vars[] = 'date_archive_month';
	$query_vars[] = 'date_archive_day';
	return $query_vars;
} );

add_action( 'template_include', function( $template ) {

	if ( get_query_var( 'archive_post_type' ) == false || get_query_var( 'archive_post_type' ) !== _POST_TYPE_SLUG ) {
		return $template;
	}

	return get_template_directory() . '/date-post-type-archive.php';
} );
Let’s update the permalinks for the archive pages

/**
 * Update Year Archive Link for cpt by prefixing 'post-type-slug'
 *
 * Previous: '/{year}/'
 * New: '/post-type-slug/{year}/'
 *
 * @param $yearlink
 * @param $year
 *
 * @return mixed|string|void
 */
function change_year_link( $yearlink, $year ) {
   if ( is_post_type_archive( POST_TYPE_SLUG ) ) {
      $modified_url_structure = sprintf( '%1$s/%2$s', POST_TYPE_SLUG, $year );
      return home_url( user_trailingslashit( $modified_url_structure ) );
   }

   return $yearlink;
}

add_filter( 'year_link', __NAMESPACE__ . '\\change_year_link', 10, 2 );

/**
 * Update Month Archive Link for cpt by prefixing 'post-type-slug'
 *
 * Previous: '/{year}/{month}/'
 * New: '/post-type-slug/{year}/{month}/'
 *
 * @param $monthlink
 * @param $year
 * @param $month
 *
 * @return mixed|string|void
 */
function change_month_link( $monthlink, $year, $month ) {
   if ( is_post_type_archive( POST_TYPE_SLUG ) ) {
      $modified_url_structure = sprintf( '%1$s/%2$s/%3$s', POST_TYPE_SLUG, $year, $month );
      return home_url( user_trailingslashit( $modified_url_structure ) );
   }

   return $monthlink;
}

add_filter( 'month_link', __NAMESPACE__ . '\\change_month_link', 10, 3 );

/**
 * Update Month Archive Link for cpt by prefixing 'post-type-slug'
 *
 * Previous: '/{year}/{month}/'
 * New: '/post-type-slug/{year}/{month}/'
 *
 * @param $monthlink
 * @param $year
 * @param $month
 *
 * @return mixed|string|void
 */
function change_day_link( $daylink, $year, $month, $day ) {

   if ( is_post_type_archive( POST_TYPE_SLUG ) ) {
      $modified_url_structure = sprintf( '%1$s/%2$s/%3$s/%4$s', POST_TYPE_SLUG, $year, $month, $day );
      return home_url( user_trailingslashit( $modified_url_structure ) );
   }

   return $daylink;
}

add_filter( 'day_link', __NAMESPACE__ . '\\change_day_link', 10, 4 );

That’s all folks!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *