Two post types share the same taxonomy in WordPress

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different…

Novembre 14, 2019

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different items of the two post types, but this action may create a few misunderstandings.

Here are some notes.

Suppose you create a post type named news and you want to use the existing post_tag taxonomy to tag also the news articles.

To add the post_tag taxonomy to the news post type you can write something like this:

function news_reg_tag() {
    register_taxonomy_for_object_type('post_tag', 'news');
}
add_action('init', 'news_reg_tag');

Wrong count in WordPress tag list

When you tag a post (an article of post type post) with a specific tag and then you tag a news (an article of post type news) with the same specific tag, when you will load the “Tags” page in WordPress “Posts” menu, you will see that the count for that specific tag is 2, but if you click on the number 2 you will see only 1 article, the one tagged in the post post type.

There are no errors. The count is 2, because 1 for post and 1 for news.

Extract all the tags (shared between two post types) used with a specific post type

Suppose you have post articles and news articles (different post types, both uses post_tag taxonomy).

To retrieve the list of tags used in post post type you have to use wp_get_object_terms and use the posts to get the tags:

$articles_in_post_type = get_posts( array(
	'fields' => 'ids',
	'post_type' => 'post',
	'posts_per_page' => -1,
));
$tags = wp_get_object_terms( $articles_in_post_type, 'post_tag', array('ids') );

If you use the get_tags function, like this:

$tags = get_tags(array(
     'hide_empty' => false
));

…it will not work, because it uses also the tag used with the news articles even if you use the hide_empty parameter. They will not be considered empty, since they are connected to news.

You can find more articles about WordPress here.

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Recommended

WP Gutenberg notes

Collection of notes and thoughts on Wordpress Gutenberg blocks development.

Gennaio 9, 2023

Test page for Bright Links plugin

To test the plugin hover your mouse to a link, or tap the link on mobile device.

Dicembre 4, 2022

Updating a metabox after Gutenberg updates

When press on "Update" refresh a custom metabox under gutenberg editor.

WP doesn’t send email? try this

Snippets for sending emails with Wordpress

Febbraio 8, 2020

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

Novembre 7, 2019

WordPress Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

How I've beat the php mysterious fatal error unknown in Wordpress caused by caching plugin WP Fastest Cache