Different Archive and Single Permalink URL

I have a custom post type to list countries but I am not sure how to create them with this permalink structure:

  • Country archive: /map
  • Country single: /map/country/england

How can I achieve this? Heres my country post code:

function countries() {

    $labels = array(
        'name'                  => _x( 'countries', 'Post Type General Name', 'countries' ),
        'singular_name'         => _x( 'country', 'Post Type Singular Name', 'countries' ),
        'menu_name'             => __( 'Countries', 'countries' ),
        'name_admin_bar'        => __( 'Country', 'countries' ),
        'parent_item_colon'     => __( 'Parent Country', 'countries' ),
        'all_items'             => __( 'All Countries', 'countries' ),
        'add_new_item'          => __( 'Add New Country', 'countries' ),
        'add_new'               => __( 'Add New Country', 'countries' ),
        'new_item'              => __( 'New Country', 'countries' ),
        'edit_item'             => __( 'Edit Country', 'countries' ),
        'update_item'           => __( 'Update Country', 'countries' ),
        'view_item'             => __( 'View Country', 'countries' ),
        'search_items'          => __( 'Search Countries', 'countries' ),
        'not_found'             => __( 'Country Not found', 'countries' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'countries' ),
        'items_list'            => __( 'Country list', 'countries' ),
        'items_list_navigation' => __( 'Countries List Navigation', 'countries' ),
        'filter_items_list'     => __( 'Filter Country List', 'countries' ),
    );
    $rewrite = array(
        'slug'                  => 'country',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
    );
    $args = array(
        'label'                 => __( 'country', 'countries' ),
        'description'           => __( 'Country Information', 'countries' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'rewrite'               => $rewrite,
        'capability_type'       => 'page',
    );
    register_post_type( 'countries', $args );

}
add_action( 'init', 'countries', 0 );

I am also trying this with the following taxonomy but it 404s even when I update the permalink settings page:

register_taxonomy(
    'region',
    'country',
    [
        'label' => __( 'Region' ),
        'rewrite' => array( 'slug' => 'map/region' ),
        'hierarchical' => true,
    ]
);

2 Answers
2

To get your desired archive URL, set has_archive to 'map' instead of true.

To get your desired single post URL, set the slug argument in your $rewrite array to 'map/country'

Leave a Comment