0

I'd like to create a custom taxonomy called "Tags", just like the native one that can be found on Posts, with the same name/slug but independent from it:

'taxonomies' => array('post_tag')

I know that I can add the line above during the CPT creation but this would make the Tags shared between posts and my CPT. That is something I'd like to avoid so I wanted to use the following code:

$labels = array(
    'name'                       => _x( 'Tags', 'taxonomy general name', 'textdomain' ),
    'singular_name'              => _x( 'Tag', 'taxonomy singular name', 'textdomain' ),
    'search_items'               => __( 'Search Tags', 'textdomain' ),
    'popular_items'              => __( 'Popular Tags', 'textdomain' ),
    'all_items'                  => __( 'All Tags', 'textdomain' ),
    'parent_item'                => null,
    'parent_item_colon'          => null,
    'edit_item'                  => __( 'Edit Tag', 'textdomain' ),
    'update_item'                => __( 'Update Tag', 'textdomain' ),
    'add_new_item'               => __( 'Add New Tag', 'textdomain' ),
    'new_item_name'              => __( 'New Tag Name', 'textdomain' ),
    'separate_items_with_commas' => __( 'Separate tags with commas', 'textdomain' ),
    'add_or_remove_items'        => __( 'Add or remove tags', 'textdomain' ),
    'choose_from_most_used'      => __( 'Choose from the most used tags', 'textdomain' ),
    'not_found'                  => __( 'No tags found.', 'textdomain' ),
    'menu_name'                  => __( 'Tags', 'textdomain' ),
);

$args = array(
    'hierarchical'          => false,
    'labels'                => $labels,
    'show_ui'               => true,
    'show_admin_column'     => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var'             => true,
    'rewrite'               => array( 'slug' => 'tag' ),
);

register_taxonomy( 'tag', 'book', $args );

My doubt is that at the bottom of the official "Register Taxonomy Page" there is a section dedicated to "Reserved Terms" and, among them, there's also "tag".

So my question is, how do I change the code written above to better comply with the Wordpress guidelines and avoid potential conflicts?

The "reserved terms" refers only to the "register_taxonomy()" function or also the 'rewrite' argument? Can I change the name of the "registered taxonomy" but keep the 'rewrite' => array( 'slug' => 'tag' ) ? Would this potentially generate conflicts?

Is enough to change the "register_taxonomy" into:

register_taxonomy( 'book_tag', 'book', $args );

but keep unchanged

'rewrite' => array( 'slug' => 'tag' )

Would this be a correct way of handling this and avoid any conflict? Is there a better way?

Thanks for your time and support

0

tag is a reserved term because it's the native query var for querying by slug with the post_tag taxonomy (see WP_Query Tag Parameters).

You could technically register your taxonomy with tag as the taxonomy name, however- you'd have to change the query_var argument to something other than tag (setting it to true uses the taxonomy name, it also accepts a string).

Anyway- it's probably safer and just all around better to use book_tag.

As for the rewrite slug being tag- if the native post_tag taxonomy is also using this slug, your new taxonomy will capture all of those requests. Wordpress will query for the slug in your book_tag taxonomy instead of post_tag.

There is a little trick to fix this- check if the requested term exists in post_tag, and make WordPress query there instead. The obvious caveat here is that you can never have terms in both taxonomies that share the same slug. To do this, we hook request, which fires before WordPress does any of its own query parsing:

function wpd_modify_tag_requests( $request ) {
    // is this a book_tag request?
    if( isset( $request['book_tag'] ) && 1 == count($request) ){
        // does the slug exist in post_tag? 
        if( get_term_by( 'slug', $request['book_tag'], 'post_tag' ) ){
            // move slug over to tag, and delete book_tag
            $request['tag'] = $request['book_tag'];
            unset( $request['book_tag'] );
        }
    }
    return $request;
}
add_filter( 'request', 'wpd_modify_tag_requests' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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