0

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!

12
  • You can use a custom field to set the taxonomy's term slug that you'd like to use in the permalink. For example, the field name could be taxonomyfoo_slug and the value would be taxonomy_z or whatever is the term slug. And use get_post_meta() to get the field value.
    – Sally CJ
    Commented Jun 17, 2019 at 8:10
  • Thank you @SallyCJ! Unfortunately I'm a newbie in programming (so sorry for this). From what I understand you're suggesting to enter the desired slug per post via a custom field? 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, since my site has thousands of posts so assigning a taxonomy in the custom field per post would be repetitive and taxing. Also for the get_post_meta() to get the field value, where can I place this in the above code? Thanks so much.
    – Bacorrito
    Commented Jun 17, 2019 at 8:31
  • You can use plugins like ACF to create the field UI without any coding. Are you already using it, or are you familiar with it? And are you using a custom post type?
    – Sally CJ
    Commented Jun 17, 2019 at 8:56
  • 1
    @SallyCJ it actually worked! :) Thanks so much for helping out on this one. By the way what are your thoughts on above question? Or should I create a new post here on stackexchange for this follow up question? Thanks again!
    – Bacorrito
    Commented Jun 17, 2019 at 10:37
  • 1
    @SallyCJ your second code also worked! Thanks so much! :)
    – Bacorrito
    Commented Jun 17, 2019 at 16:09

1 Answer 1

0

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';
}
2
  • Wow thanks @Sally CJ! let me check this out.
    – Bacorrito
    Commented Jun 17, 2019 at 10:28
  • 1
    It actually worked! :)
    – Bacorrito
    Commented Jun 17, 2019 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.