The solution I’m looking for is for a specific taxonomy item to be the
default/priority in case it’s ticked with other taxonomy items
Are you actually looking for something like this:
$default_slug = 'taxonomy_z'; // slug of the default term
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$list = wp_list_filter( $terms, array( 'slug' => $default_slug ) );
if ( ! empty( $list ) ) { // the post **is** in the default term
// If two or more terms assigned to the post, use the default term's slug.
$taxonomy_slug = ( count( $terms ) > 1 ) ? $default_slug : $terms[0]->slug;
} else { // the post is **not** in the default term
$taxonomy_slug = $terms[0]->slug;
}
} else { // the post is not in any of the taxonomy's terms; use the default term.
$taxonomy_slug = $default_slug;
}
That would replace this part in your code:
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'taxonomy_z';
UPDATE
if taxonomy “nashville” became a parent taxonomy and its neighborhoods
“hillsboro” and “greenhills” became its child taxonomies, is there
also a way to turn to “nashville” as their default url slug?
Yes, by storing the parent terms (their slugs) in an array ($default_slugs
in the below example). However, this means that your posts should not have two parent terms which are in that array. I.e. A post can be in tennessee
or nashville
, but not both. Because otherwise, the permalink slug might not be what you expected it to be.
$default_slugs = array( 'tennessee', 'nashville' ); // list of parent terms/slugs
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$slugs = wp_list_pluck( $terms, 'slug' );
$list = array_intersect( $slugs, $default_slugs );
if ( ! empty( $list ) ) { // the post **is** in a parent term
// If two or more terms assigned to the post, use a parent term's slug.
$default_slug = array_shift( $list );
$taxonomy_slug = ( count( $terms ) > 1 ) ? $default_slug : $terms[0]->slug;
} else { // the post is **not** in a parent term
$taxonomy_slug = $terms[0]->slug;
}
} else { // the post is not in any of the taxonomy's terms; use a default term.
$taxonomy_slug = 'tennessee';
}