First off this is my first time posting on here. I can usually do research and put together code that I need based off of other’s responses, but I’m stumped on this one. I’ve been at this for almost a week.
I’m using the standard wp post type and categories. I have a custom taxonomy called studio. Some of the posts are auto created during an import, which I select the category to import into. The problem is that after they posts are imported I have to go back and manually select the custom taxonomy, ‘studio’. Some of the categories I have are also listed in the studio taxonomy.
I need to get the ‘category’; check to see if it exists in ‘studio’ taxonomy; if yes; then add it to the ‘studio’ taxonomy for that post.
category | studio | post |
---|---|---|
Apples | Apples | Add |
Oranges | Oranges | Add |
House | skip | |
Tv | skip | |
Peaches | Peaches | Add |
Pears | Pears | Add |
Table | skip |
I tried the AutoTagger plugin but that only searches in title, post and excerpt for provided terms. I’ve also tried numerous codes too many to list, but none worked.
This is what I have that partly works but doesn’t add the ‘category’. It adds the word category to the ‘studio’ taxonomy.
add_action( 'save_post', 'studio_code_save_post', 10, 2 );
function studio_code_save_post( $post_ID, $post ) {
$post_categories = get_the_terms( $post->ID, 'category' );
if ( 'post' != $post->post_type || wp_is_post_revision( $post_ID ) )
return;
wp_set_object_terms( $post_ID, 'category', 'studio' );
}
If I change
wp_set_object_terms( $post_ID, 'category', 'studio' );
to
wp_set_object_terms( $post_ID, $post_categories, 'studio' );
It adds a blank term to the ‘studio’ taxonomy.
Thanks for any help.