Archived topic
CPT Taxonomy Function Applying to CPT and Standard Posts
6 replies · Started by PJ on February 16, 2022
Hi there,
We added a CPT with the register_taxonomy_for_custom_object function, imported posts from another site, used Post Type Switcher to move them to the new CPT, but for some reason the taxonomy and tags are attached to both the default posts and the new CPT. The category & tag archives show up empty since the switch. Any ideas on what I may be missing?
/* Trends Custom Post Types */
add_theme_support('post-thumbnails');
add_post_type_support( 'trend', 'thumbnail' );
function create_trends_posttype() {
register_post_type( 'trend',
// CPT Options
array(
'labels' => array(
'name' => __( 'Trends' ),
'singular_name' => __( 'Trend' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'trends'),
'show_in_rest' => true,
'menu_icon' => 'dashicons-star-empty',
'taxonomies' => array('category', 'post_tag')
)
);
}
add_action( 'init', 'create_trends_posttype');
add_action('init', 'add_category_tags_to_cpt');
function add_category_tags_to_cpt() {
register_taxonomy_for_object_type('category', 'trend');
register_taxonomy_for_object_type('post_tag', 'trend');
}
Thank you,
Becky Sue
Hi Becky,
It may be best to ask Post Type Switcher's support for this as the post switching process likely included the taxonomy you currently as well as it was part of the original state of the post's meta from the other site BEFORE you imported it.
I'm not sure if Post type switcher has a feature that removes the taxonomy when it switches the post type. Perhaps they have one (if they have some sort of a pro version). It's worth asking. :)
Hi Elvin,
Thanks so much for the reply! So it looks like the plugin is no longer supported; an old reply indicated that the tags should have carried over and been rewritten with the functions we had in place.
In any case, I'm curious if you know of another solution that would allow us to import a post type into a different post type?
Hey again,
Looking at the categories and tags...If I add a category to the CPT categories - it also adds to the default post categories. The archive URLs are the same, and the data is interconnected, which makes me think it's an issue with the code, not the plugin. Am I missing something?
Thank you,
Becky Sue
The archive URLs are the same, and the data is interconnected, which makes me think it’s an issue with the code, not the plugin. Am I missing something?
I won't rule the code out just yet.
To clarify a few things:
Are the post already converted and is published to the site? The post type archive for /trends/ is empty when I checked.
If I may suggest:
While I understand that you've already able to do CPT w/ PHP coding, code's management of the CPT's meta and other things get tricky.
Consider using plugins like CPT UI for this.
CPT UI lets you manage the taxonomy assignment as shown in this screenshot -
https://share.getcloudapp.com/nOu9xPK6
Thanks Elvin. That looks like a super useful tool.
We ended up adding custom taxonomies and re-importing.
Thank you for your help.
No problem. Let us know if you need further help. :D