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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Here’s an (untested) example on how to get the saved post’s categories, check if any of them exists in another taxonomy, and terms that exists to the post. Instead of the generic save_post action you can use the post type specific one, save_post_{post_type}. If you want to prevent the check from running every time the post is saved / updated, you can utilize the third parameter the hook provides, which tells you is the action for a new or an existing post.
// hook to post type specific save action
add_action( 'save_post_post', 'studio_code_save_post', 10, 3 );
function studio_code_save_post( $post_ID, $post, $update ) {
if ( wp_is_post_revision( $post ) ) {
return;
}
// uncomment to prevent code running on post update
// if ( $update ) {
// return;
// }
$categories = get_the_terms( $post, 'category' );
$studio_terms = array();
if ( $categories && is_array($categories) ) {
foreach ($categories as $category) {
// check, if the term exists in another taxonomy
if ( term_exists( $category->slug, 'studio' ) ) {
$studio_terms[] = $category->slug;
}
}
}
if ( $studio_terms ) {
// append terms to post
wp_set_object_terms( $post_ID, $studio_terms, 'studio', true );
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0