最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - How to assign a defaultpriority taxonomy to be shown in url in case two taxonomy items are selected

programmeradmin1浏览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/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
  • 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
 |  Show 7 more comments

1 Answer 1

Reset to default 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';
}
发布评论

评论列表(0)

  1. 暂无评论