This problem has been for bugging me for days and I feel that I am close!
Creating a review website, I have custom post type called review
, organized in a custom hierarchical taxonomy niche
. Put simply, my end goal is for my URLs to look like this:
mysite.com/coffee-maker/
: shows niche page template
mysite.com/coffee-maker/keurig-k425
: shows individual review
I have tried the solution offered here: http://wordpress.stackexchange.com/a/5313/1685, which led to two problems:
- CPTs worked fine but all my pages and posts return 404 errors.
Looking at the Rewrite Analyser, I realized they are routed to the
"niche:" rule. I realize there might be a conflict but don't know
where to go from here!
- Intra-page taxonomy links such as those generated using
get_category_link()
still append the/niche/
prefix to URL, which make them 404 too.
I feel I am close to finding what I need but saddened by the fact that this seems beyond me. Thanks a lot!
My chunks of code:
function cptui_register_my_cpts_review() {
/**
* Post Type: Reviews.
*/
$labels = array(
"name" => __( "Reviews", "" ),
"singular_name" => __( "Review", "" ),
"menu_name" => __( "Mes Reviews", "" ),
"all_items" => __( "Toutes les Reviews", "" ),
"add_new" => __( "Ajouter une Review", "" ),
"add_new_item" => __( "Ajouter une Review", "" ),
"edit_item" => __( "Modifier la Review", "" ),
"new_item" => __( "Nouvelle Review", "" ),
"view_item" => __( "Voir la Review", "" ),
"view_items" => __( "Voir les Reviews", "" ),
"search_items" => __( "Rechercher une Review", "" ),
"not_found" => __( "Pas de Review Trouvée", "" ),
"not_found_in_trash" => __( "Pas de Review Trouvée dans Corbeille", "" ),
"parent_item_colon" => __( "Review Parente", "" ),
"insert_into_item" => __( "Insérer dans la Review", "" ),
"uploaded_to_this_item" => __( "Insérer dans la Review", "" ),
"filter_items_list" => __( "Filtrer la Liste des Reviews", "" ),
"items_list_navigation" => __( "Navigation dans la Liste des Reviews", "" ),
"items_list" => __( "Liste des Reviews", "" ),
"attributes" => __( "Attributs de la Review", "" ),
"parent_item_colon" => __( "Review Parente", "" ),
);
$args = array(
"label" => __( "Reviews", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "%niche%", "with_front" => true ),
"query_var" => false,
"menu_icon" => "dashicons-awards",
"supports" => array( "title", "editor", "thumbnail", "excerpt", "custom-fields", "genesis-seo" ),
"taxonomies" => array( "niche" ),
);
register_post_type( "review", $args );
}
add_action( 'init', 'cptui_register_my_cpts_review' );
function cptui_register_my_taxes_niche() {
/**
* Taxonomy: Niches.
*/
$labels = array(
"name" => __( "Niches", "" ),
"singular_name" => __( "Niche", "" ),
"menu_name" => __( "Niches", "" ),
"all_items" => __( "Toutes les niches", "" ),
"edit_item" => __( "Modifier la niche", "" ),
"view_item" => __( "Voir la niche", "" ),
"update_item" => __( "Mettre à jour le nom de la niche", "" ),
"add_new_item" => __( "Ajouter nouvelle niche", "" ),
"new_item_name" => __( "Nom de la nouvelle niche", "" ),
"parent_item" => __( "Niche parente", "" ),
"parent_item_colon" => __( "Niche parente :", "" ),
"search_items" => __( "Rechercher les niches", "" ),
"popular_items" => __( "Niches Populaires", "" ),
"separate_items_with_commas" => __( "Séparer les niches avec des virgules", "" ),
"add_or_remove_items" => __( "Ajouter ou enlever des Niches", "" ),
"choose_from_most_used" => __( "Choisir parmi les plus utilisées", "" ),
"not_found" => __( "Pas trouvé !", "" ),
"no_terms" => __( "Pas de niches", "" ),
"items_list_navigation" => __( "Navigation par liste des niches", "" ),
"items_list" => __( "Liste des niches", "" ),
);
$args = array(
"label" => __( "Niches", "" ),
"labels" => $labels,
"public" => true,
"hierarchical" => true,
"label" => "Niches",
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'niche', 'with_front' => false, 'hierarchical' => false ),
"show_admin_column" => false,
"show_in_rest" => false,
"rest_base" => "",
"show_in_quick_edit" => true,
);
register_taxonomy( "niche", array( "review" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_niche' );
function wpse_5308_post_type_link( $link, $post ) {
if ( $post->post_type === 'review' ) {
if ( $terms = get_the_terms( $post->ID, 'niche' ) )
$link = str_replace( '%niche%', current( $terms )->slug, $link );
}
return $link;
}
add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );