I have a custom taxonomy (ex. taxonomyfoo) created with CPT UI plugin and assigned this taxonomy to the standard wp post.
I have also used below code to add the taxonomy slug in the url (%taxonomyfoo%/%post-title%/):
add_filter('post_link', 'taxonomyfoo_permalink', 10, 3);
add_filter('post_type_link', 'taxonomyfoo', 10, 3);
function taxonomyfoo_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%taxonomyfoo%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'taxonomyfoo');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'taxonomy_z';
return str_replace('%taxonomyfoo%', $taxonomy_slug, $permalink);
}
If I only have two items under taxonomyfoo (taxonomy_z and taxonomy_a), and knowing that the order of taxonomies in wp is alphabetical, if I select both taxonomy_z and taxonomy_a for a single post, it will automatically use taxonomy_a in the url (example/taxonomy_a/post-title/).
Is there a way to make taxonomy_z the default/priority url slug in case all taxonomies are selected? Because in some posts I have scenarios when both need to be selected however one is more of a priority to be present the url than the other.
Much thanks!
I have a custom taxonomy (ex. taxonomyfoo) created with CPT UI plugin and assigned this taxonomy to the standard wp post.
I have also used below code to add the taxonomy slug in the url (%taxonomyfoo%/%post-title%/):
add_filter('post_link', 'taxonomyfoo_permalink', 10, 3);
add_filter('post_type_link', 'taxonomyfoo', 10, 3);
function taxonomyfoo_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%taxonomyfoo%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'taxonomyfoo');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'taxonomy_z';
return str_replace('%taxonomyfoo%', $taxonomy_slug, $permalink);
}
If I only have two items under taxonomyfoo (taxonomy_z and taxonomy_a), and knowing that the order of taxonomies in wp is alphabetical, if I select both taxonomy_z and taxonomy_a for a single post, it will automatically use taxonomy_a in the url (example/taxonomy_a/post-title/).
Is there a way to make taxonomy_z the default/priority url slug in case all taxonomies are selected? Because in some posts I have scenarios when both need to be selected however one is more of a priority to be present the url than the other.
Much thanks!
Share Improve this question asked Jun 17, 2019 at 7:37 BacorritoBacorrito 54 bronze badges 12 | Show 7 more comments1 Answer
Reset to default 0The 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';
}
taxonomyfoo_slug
and the value would betaxonomy_z
or whatever is the term slug. And useget_post_meta()
to get the field value. – Sally CJ Commented Jun 17, 2019 at 8:10