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.com/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!
taxonomyfoo_slug
and the value would betaxonomy_z
or whatever is the term slug. And useget_post_meta()
to get the field value.