Post custom permalink results in 404 for archive page

You can override the Post type object settings in the init action and register the proper values for the archive and rewrites:

add_action('init', 'my_init');

function my_init()
{
   $args = objectToArray( get_post_type_object('post') );

   $args['has_archive'] = 'news';
   $args['rewrite'] = array(
      'slug' => 'news',
      'with_front' => FALSE,
   );

   register_post_type('post', $args);
}

function objectToArray( $object )
{
   if( !is_object( $object ) && !is_array( $object ) )
   {
      return $object;
   }

   if( is_object( $object ) )
   {
      $object = get_object_vars( $object );
   }

   return array_map('objectToArray', $object);
}

By specifying the has_archive and the rewrite values for the Post type object it’s possible to force WordPress the same behavior as one would do for Custom Post types.

WordPress should now pick up the archive.php template when browsing to /news.

The only clumsy thing about this is of course that get_post_type_object returns an object version of the array that register_post_type takes as a parameter, hence the objectToArray helper function.

Leave a Comment