I have a custom post type and a custom taxonomy. My desired permalink structure is:
Main archive page: /custom-post-type (or /how-to/) Taxonomy archive page: /custom-post-type/taxonomy (or /how-to/taxonomy) Single post page: /custom-post-type/taxonomy/post (or /how-to/taxonomy/post)
The bottom 2 are working but the main archive page points to "/how-to/%how-to_types%" and doesn't work. Any help would greatly be appreciated.
// How-tos
// Register Custom Post Type How-to
function create_howto_cpt() {
$labels = array(
'name' => _x( 'How-tos', 'Post Type General Name', 'how-tos' ),
'singular_name' => _x( 'How-to', 'Post Type Singular Name', 'how-tos' ),
'menu_name' => _x( 'How-tos', 'Admin Menu text', 'how-tos' ),
'name_admin_bar' => _x( 'How-to', 'Add New on Toolbar', 'how-tos' ),
'archives' => __( 'How-to Archives', 'how-tos' ),
'attributes' => __( 'How-to Attributes', 'how-tos' ),
'parent_item_colon' => __( 'Parent How-to:', 'how-tos' ),
'all_items' => __( 'All How-tos', 'how-tos' ),
'add_new_item' => __( 'Add New How-to', 'how-tos' ),
'add_new' => __( 'Add New', 'how-tos' ),
'new_item' => __( 'New How-to', 'how-tos' ),
'edit_item' => __( 'Edit How-to', 'how-tos' ),
'update_item' => __( 'Update How-to', 'how-tos' ),
'view_item' => __( 'View How-to', 'how-tos' ),
'view_items' => __( 'View How-tos', 'how-tos' ),
'search_items' => __( 'Search How-to', 'how-tos' ),
'not_found' => __( 'Not found', 'how-tos' ),
'not_found_in_trash' => __( 'Not found in Trash', 'how-tos' ),
'featured_image' => __( 'Featured Image', 'how-tos' ),
'set_featured_image' => __( 'Set featured image', 'how-tos' ),
'remove_featured_image' => __( 'Remove featured image', 'how-tos' ),
'use_featured_image' => __( 'Use as featured image', 'how-tos' ),
'insert_into_item' => __( 'Insert into How-to', 'how-tos' ),
'uploaded_to_this_item' => __( 'Uploaded to this How-to', 'how-tos' ),
'items_list' => __( 'How-tos list', 'how-tos' ),
'items_list_navigation' => __( 'How-tos list navigation', 'how-tos' ),
'filter_items_list' => __( 'Filter How-tos list', 'how-tos' ),
);
$args = array(
'label' => __( 'how-to', 'how-tos' ),
'description' => __( 'How-to articles', 'how-tos' ),
'labels' => $labels,
'menu_icon' => 'dashicons-lightbulb',
'supports' => array('title', 'author', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions'),
'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,
'hierarchical' => true,
'exclude_from_search' => false,
'show_in_rest' => true,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array('slug' => 'how-to/%how-to_types%'),
);
register_post_type( 'how-to', $args );
}
add_action( 'init', 'create_howto_cpt', 0 );
// Register How-to Taxonomy
function create_howto_tax() {
$labels = array(
'name' => _x( 'Types', 'taxonomy general name' ),
'singular_name' => _x( 'Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Types' ),
'all_items' => __( 'All Types' ),
'parent_item' => __( 'Parent Type' ),
'parent_item_colon' => __( 'Parent Type:' ),
'edit_item' => __( 'Edit Type' ),
'update_item' => __( 'Update Type' ),
'add_new_item' => __( 'Add New Type' ),
'new_item_name' => __( 'New Type Name' ),
'menu_name' => __( 'Types' ),
);
register_taxonomy('how-to_types',array('how-to'), array(
'hierarchical' => true,
'labels' => $labels,
'show_in_rest' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'how-to', 'with_front' => false),
));
}
add_action( 'init', 'create_howto_tax', 0 );
// Fixing permalinks
function how_to_permalinks( $post_link, $id = 0 ){
$post = get_post($id);
$terms = wp_get_object_terms( $post->ID, 'how-to_types' );
if( $terms ){
return str_replace( '%how-to_types%' , $terms[0]->slug , $post_link );
} else {
return str_replace( '%how-to_types%/' , '' , $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'how_to_permalinks', 1, 3 );
Please update has_archive
parameter within register_post_type
arguments
From 'has_archive' => true,
to 'has_archive' => 'how-to',
and update your permalink settings to regenerate rewrite rules and check, Hope this will help you!