Wordpress Nav Error Undefined property: WP_Post_Type::$term_id

Viewed 710

I've run into a problem that it looks like a few people have faced but no one has posted a solution for.

The problem occurs after creating a Custom Post Type & Custom Taxonomy when I use the wp_nav_menu() function. I only see this error on my archive page, where I get the error:

Notice: Undefined property: WP_Post_Type::$term_id in C:\WebServer\Bolton\wp-includes\nav-menu-template.php on line 350

The problem stops when I change my taxonomy hierarchical to false, although I need it true. I've also been able to stop the error by editing the WordPress core file:

/wp-includes/nav-menu-template.php

Lines 349-361, if I wrap it in is_single()

// LINES 349 - 361

if (is_single()) {
    $desc = $queried_object->term_id;
    do {
        $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
        if ( isset( $term_to_ancestor[ $desc ] ) ) {
            $_desc = $term_to_ancestor[ $desc ];
            unset( $term_to_ancestor[ $desc ] );
            $desc = $_desc;
        } else {
            $desc = 0;
        }
    } while ( ! empty( $desc ) );
}

So nav-menu-template is unable to get a queried object ID because it's on an archive page and giving an error because of it. The menu still displays correctly although with a big notice right across the navigation.

The notice goes away with my edited Wordpress nav-menu-template file, although a version update will break that so I need to fix the core problem.

The call to wp_nav_menu() can be plain or with any parameter (name, ID, etc.) the error is the same.

Thank you for your time.

Ps. This is my first posted question, I apologise if I've done anything incorrectly.

Below is my CPT & Taxonomy registration:

// Register CPT's
$args = [
    'label' => 'Services',
    'public' => true,
    'has_archive' => true,
    'menu_icon' => 'dashicons-hammer',
    'taxonomy'  => 'locations'
];
register_post_type ("Services", $args);

// Register taxonomies
$labels = [
    'name'  => 'Locations',
    'singular_name' => 'Location',
];
$args = [
    'hierarchical' => true,
    'labels' => $labels,
    'public' => true,
    'query_var' => true,
];
register_taxonomy ("locations", "services", $args);
0 Answers
Related