0

I have a 'house' cpt and a 'locations' hierarchical custom taxonomy. Permalinks are set to

/%category%/%postname%

The terms in the hierarchical taxonomy are like:

usa >> new-york

germany >> berlin

The code in my functions.php:

// Register Taxonomy locations
function register_taxonomy_locations() {

$labels = array(
    'name'                       => _x( 'Locations', 'Taxonomy General Name', 'genesis' ),
    'singular_name'              => _x( 'Location', 'Taxonomy Singular Name', 'genesis' ),
    'menu_name'                  => __( 'Locations', 'genesis' ),
    'all_items'                  => __( 'All Locations', 'genesis' ),
    'parent_item'                => __( 'Parent Location', 'genesis' ),
    'parent_item_colon'          => __( 'Parent Location:', 'genesis' ),
    'new_item_name'              => __( 'New Location Name', 'genesis' ),
    'add_new_item'               => __( 'Add New Location', 'genesis' ),
    'edit_item'                  => __( 'Edit Location', 'genesis' ),
    'update_item'                => __( 'Update Location', 'genesis' ),
    'view_item'                  => __( 'View Location', 'genesis' ),
    'separate_items_with_commas' => __( 'Separate Locations with commas', 'genesis' ),
    'add_or_remove_items'        => __( 'Add or remove Locations', 'genesis' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'genesis' ),
    'popular_items'              => __( 'Popular Locations', 'genesis' ),
    'search_items'               => __( 'Search Locations', 'genesis' ),
    'not_found'                  => __( 'Not Found', 'genesis' ),
    'no_terms'                   => __( 'No Locations', 'genesis' ),
    'items_list'                 => __( 'Locations list', 'genesis' ),
    'items_list_navigation'      => __( 'Locations list navigation', 'genesis' ),
);
$rewrite = array(
    'slug'                       => 'houses',
    'with_front'                 => true,
    'hierarchical'               => false,
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => true,
    'rewrite'                    => $rewrite,
);
register_taxonomy( 'locations', array( 'house' ), $args );

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

// Register CPT House
function register_cpt_house() {

$labels = array(
    'name'                  => _x( 'Houses', 'Post Type General Name', 'genesis' ),
    'singular_name'         => _x( 'House', 'Post Type Singular Name', 'genesis' ),
    'menu_name'             => __( 'House', 'genesis' ),
    'name_admin_bar'        => __( 'Houses', 'genesis' ),
    'archives'              => __( 'House Archives', 'genesis' ),
    'attributes'            => __( 'House Attributes', 'genesis' ),
    'parent_item_colon'     => __( 'Parent House:', 'genesis' ),
    'all_items'             => __( 'All Houses', 'genesis' ),
    'add_new_item'          => __( 'Add New House', 'genesis' ),
    'add_new'               => __( 'Add New', 'genesis' ),
    'new_item'              => __( 'New House', 'genesis' ),
    'edit_item'             => __( 'Edit House', 'genesis' ),
    'update_item'           => __( 'Update House', 'genesis' ),
    'view_item'             => __( 'View House', 'genesis' ),
    'view_items'            => __( 'View Houses', 'genesis' ),
    'search_items'          => __( 'Search House', 'genesis' ),
    'not_found'             => __( 'Not found', 'genesis' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'genesis' ),
    'featured_image'        => __( 'Featured Image', 'genesis' ),
    'set_featured_image'    => __( 'Set featured image', 'genesis' ),
    'remove_featured_image' => __( 'Remove featured image', 'genesis' ),
    'use_featured_image'    => __( 'Use as featured image', 'genesis' ),
    'uploaded_to_this_item' => __( 'Uploaded to this House', 'genesis' ),
    'items_list'            => __( 'Houses list', 'genesis' ),
    'items_list_navigation' => __( 'Houses list navigation', 'genesis' ),
    'filter_items_list'     => __( 'Filter Houses list', 'genesis' ),
);
$rewrite = array(
    'slug'                  => 'house',
    'with_front'            => true,
    'pages'                 => true,
    'feeds'                 => true,
);
$args = array(
    'label'                 => __( 'House', 'genesis' ),
    'description'           => __( 'CPT House', 'genesis' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', ),
    'taxonomies'            => array( 'locations' ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-admin-home',
    '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'       => 'post',
    'query_var'             => true,
    'permalink_epmask'      => EP_PERMALINK,
    'show_in_rest'          => false,
);

register_post_type( 'house', $args );

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

// Add Archive Settings option to house CPT
add_post_type_support( 'house', 'genesis-cpt-archives-settings' );

So, for a single house post URL is https://website.com/house/house-1, for a parent term page URL should be https://website.com/houses/usa and for a sub-term page should be https://website.com/houses/new-york

When viewing any house, the URL is https://website.com/house/house-1, https://website.com/house/house-2, etc, which is correct.

When viewing a parent term page, like https://website.com/houses/usa or https://website.com/houses/germany, it shows the houses in those parent terms, which is correct.

But when viewing a sub-term page, like https://website.com/houses/new-york or https://website.com/houses/berlin, it's a 404. I can't find why.

Of course, https://website.com/houses/usa/new-york or https://website.com/usa/new-york don't work either, but I don't want those.

If, instead of writting the code myself in functions.php, I use any plugin such as Types or Custom Post Type UI with the same requirements, I can see any of the taxonomy pages with no problems at all.

I'm sure this has to be a very simple issue, but I can't see it.

Thanks in advance

** UPDATED **

The code above works well when there's only one taxonomy for one cpt. The issue appears when you add another taxonomy with the same code as above (obviously changing to the new taxonomy name). For the sake of brevity, I described the issue with one taxonomy. Sorry for that, I should have specified that there are more taxonomies involved. With every taxonomy, I need to have the same slug for every term, like:

https://website.com/houses/term-of-taxonomy-A/
https://website.com/houses/sub-term-of-taxonomy-A/
https://website.com/houses/term-of-taxonomy-B/
https://website.com/houses/sub-term-of-taxonomy-B/
  • Your code works as expected for me, child terms are not 404s. Are you sure those terms are assigned to posts? – Milo Sep 5 '17 at 0:34
  • Sorry for answering this late, I was traveling and no wi-fi. Yup, some are, others not. In fact, the latter shouldn't get a 404. They should show a sub-term page for that sub-term saying something like: "Sorry, no content matched your criteria.", not a 404. This is a very basic setup, latest WP with Genesis Sample theme, Query Monitor, FacetWP and no complications. The only thing is that all terms were imported (just the terms, no posts or anything) from an .xml file from another clean install. Flushed permalinks, so I don't know. – Karls Sep 5 '17 at 19:57
  • @Milo, this is what happens: for the sake of brevity, I mentioned there's a cpt and a taxonomy (just to simplify the code). With one taxonomy, all works well. But (sorry, really) in reality there are seven taxonomies, every one of them for types of locations. So, instead of one 'locations' taxonomy, I have 'admin-locations' for administrative locations, such as country, state, etc, 'natural-locations' for mountain ranges, etc., up to seven types. So when there's more than one location type for the same cpt, 404 everywhere. Even with no imports, just adding a couple of terms with taxonomy empty – Karls Sep 5 '17 at 21:53
  • Does each of the taxonomies have a unique slug? You can't share a slug between multiple taxonomies / post types, WordPress determines what to query by the URL pattern, so whatever taxonomy is registered last will override any previous taxonomy with the same slug. – Milo Sep 6 '17 at 2:54
  • The only way to share a slug is to resolve all of those requests manually, by determining which taxonomy it comes from before the main query is run. Not especially efficient. Every taxonomy would also have to have all terms unique to each taxonomy, or there would never be a way to determine which is meant to be queried. – Milo Sep 6 '17 at 3:08

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.