2

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.

2
  • Can you do a var_dump( $post_categories ) to see what that's actually getting? Apr 28, 2021 at 14:56
  • @Tony Djukic I didn't think about dong a var_dump. Thanks for the recommendation. I will keep that in mind. Apr 29, 2021 at 12:37

1 Answer 1

2

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 );
    }
}
1

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.