Primary Taxonomy for Post

My blog posts have have multiple items from my custom taxonomy attached to them. For the sake of this post lets just use Tags taxonomy...

So a post may contain 4 tags, is there a way I can select one tag as the primary tag for the post?

Topic advanced-taxonomy-queries tags custom-taxonomy Wordpress

Category Web


This post is super old but I'll post my solution here in case anyone else stumbles across this.

If you're using Yoast SEO you can hook into their existing implementation (they do this already for categories).

function add_variety_primary_term_taxonomy( $taxonomies, $post_type, $all ) {
    if( $post_type == 'product' && isset( $all['my_custom_tax'] ) ) {
        $taxonomies['my_custom_tax'] = $all['my_custom_tax'];
    }

    return $taxonomies;
}

add_filter( 'wpseo_primary_term_taxonomies', 'add_variety_primary_term_taxonomy', 10, 3 );

Also, your custom taxonomy must have 'hierarchical' => true for this to work.

Good luck!


No, you cannot. Taxonomies, categories, and terms all carries the same rank in the hierarchy when they are at the same level. If you attach a post to 4 taxonomies, categories, or terms on the same level, all these taxonomies will be equal in rank.

The only way you can achieve what you are trying to do, is to create a parent with a few children under that parent, attach the post to the parent and a couple of children. That parent will then be the "primary" tag for the post

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.