0

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 );
0

This one has tripped me up in the past. If you have a collision between a taxonomy and post type rewrite, it's important that you register the taxonomy before the post type.

Assuming you'd be ok with reworking your reviews URLs to the format /reviews/%niche%/%postname%, you'd need to change your post type as follows:

'rewrite' => array( 'slug' => 'reviews/%niche%', 'with_front' => false ),
'has_archive' => 'reviews',

your taxonomy:

'rewrite' => array( 'slug' => 'reviews', 'with_front' => false ),

and as mentioned above, do your call to register_taxonomy() before your call to register_post_type(). Visit Settings > Permalinks and resave to flush your rewrite rules.

  • Thank you Chris. It seems that I have a collision not between my "niche" taxonomy and "review" post type, but between one of these and my regular pages/posts. For instance, the Rewrite Analyser for something like mysite.com/faq (which I would like to point to the existing FAQ page) returns "niche: faq". Actual URL returns a 404 page. I understand that my site interprets it as an item from the "niche" taxonomy, but being the newbie I am, I don't know why and how to change this. – Nicolas Telmaut Aug 10 '17 at 15:49
  • You may have to rework your desired url structure to something like: mysite.com/reviews/coffee-maker/ mysite.com/reviews/coffee-maker/keurig-k425 Would that be acceptable? – Chris Cox Aug 10 '17 at 16:03
  • (using this : wordpress.stackexchange.com/questions/16902/…) I am now displaying pages! Need to find the same code for posts and that should do it. Being a newbie, I wonder if these solutions are futureproof in terms of site loadtime and potential 404 problems... – Nicolas Telmaut Aug 10 '17 at 16:06
  • Chris, I would be open to such a structure if there was no way around it to make a "futureproof" code. Can I ask how you would go about doing this? – Nicolas Telmaut Aug 10 '17 at 16:07
  • I'll edit the info into my answer above, as comments are awful for posting code. – Chris Cox Aug 10 '17 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.