0

I have a custom taxonomy, "type", which is 'hierarchical' => true,.

Terms include:

  • "Industry moves"
  • "Market indicators"
  • "Viewpoints"
    • "Analyst view"
    • "Executive view"
    • "Interview"

I have a taxonomy.php theme file and I want these pages to be accessible at

http://www.example.com/type/viewpoints (parent term) but also eg.

http://www.example.com/type/viewpoints/executive-view (sub-terms)

I have a $rewrite. When I set 'hierarchical' => false, of course, I can only access those sub-terms at their single-level URLs eg.

http://www.example.com/type/executive-view

When I flip it to 'hierarchical' => true, I can successfully access the page at the nested URL structure http://www.example.com/type/viewpoints/executive-view

But (and here is the problem), this messes up the page for the parent term. Specifically, http://www.example.com/type/viewpoints then generates a 404 (actually, points back to index).

How can I both have my cake and eat it - that is, how can I access a page for both sub-terms and their parent term?

At each step, I have saved permalinks in the admin.

Here is the code I am using to register the taxonomy in a plugin...

<?php
/**
 * ==============================================================================
 *                      REGISTER TAXONOMY
 * ==============================================================================
 */

if ( ! function_exists( 'register_taxonomy_type' ) ) {

function register_taxonomy_type() {

  $labels = array(
        'name'                       => _x( 'Types', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Types', 'text_domain' ),
        'all_items'                  => __( 'All Types', 'text_domain' ),
        'parent_item'                => __( 'Parent Type', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
        'new_item_name'              => __( 'New Type Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Type', 'text_domain' ),
        'edit_item'                  => __( 'Edit Type', 'text_domain' ),
        'update_item'                => __( 'Update Type', 'text_domain' ),
        'view_item'                  => __( 'View Type', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove Type', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular Type', 'text_domain' ),
        'search_items'               => __( 'Search Type', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No Type', 'text_domain' ),
        'items_list'                 => __( 'Types list', 'text_domain' ),
        'items_list_navigation'      => __( 'Types list navigation', 'text_domain' ),
        );
    $rewrite = array(
        'slug'                       => 'type',
        'with_front'                 => true,
        'hierarchical'               => false,
    );
        $args = array(
            'labels'                     => $labels, // as above
            'public'                     => true,
            'show_ui'                    => true,
            'show_in_nav_menus'          => true,
            'hierarchical'               => true,
            'show_admin_column'          => true,
        'single_value'               => false, // Use single-select radio button, only one Type per object
            'show_tagcloud'              => true,
            'rewrite'                    => $rewrite,// as above
        );
    // Put it all together!
    register_taxonomy(
        /* taxonomy name */         'type',
        /* attach to object */  array( 'article' ),
        /* arguments */                 $args
    );

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

}
?>
  • 1
    This may be related to the fact that type is already used by the post_format taxonomy and is on the list of reserved terms. – Milo Nov 12 '18 at 16:03
  • @Milo - this seems like a good shout... I changed the taxonomy name from "type" to "format", flushed the permalinks and it seems like I can access term example.com/format/viewpoint as well as sub-term example.com/format/viewpoint/interview – Robert Andrews Nov 12 '18 at 20:19

Your Answer

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

Browse other questions tagged or ask your own question.