0

I've been searching for about 2 hours now on a solution of copying/moving selected taxonomy values for each posts to another. Here's the situation.

I'm working on a site that has a post type of articles and I'm moving them back to the innate post post type. Within the articles, I've had a taxonomy that was for the categories and I've successfully moved over the terms to the default categories taxonomy for the post type.

Now, I'm ready to bulk convert the posts (articles -> post) which isn't a problem. But I need to have the selected terms that were checked in the old category taxonomy also checked/marked in the new one.

Is there any way to achieve this in a bulk fashion. I don't want my clients to have to go through every individual post and remark their category terms.

Thank you in advance!

2
  • You want a SQL query for achieving this?
    – m4n0
    May 9, 2015 at 16:13
  • @ManojKumar - Sure, that would be great!
    – Mikel
    May 9, 2015 at 16:41

3 Answers 3

1

This will get all posts with $old_tax, copy the terms from $old_tax to $new_tax, and then set the $new_tax terms for those posts.

<?php
    function convert_tax($old_tax, $new_tax) {
        $pages = get_posts(array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => $old_tax,
                    'operator' => 'EXISTS'
                )
            )
        ));

        foreach($pages as $post) {
            $terms = get_the_terms( $post->ID, $old_tax );
            $term = array();

            foreach( $terms as $t ) {
                if( get_term_by( 'name', $t->name, $new_tax ) == false ) {
                    wp_insert_term( $t->name, $new_tax, $args = array() );
                    wp_set_post_terms($post->ID, array(intval($t->ID)), $new_tax ); 
                }
            }
        }
    }

    convert_tax('policy_area', 'topic')

?>
2
  • For custom post types use wp_set_object_terms and not wp_set_post_terms. Feb 6, 2020 at 13:22
  • And for custom posts the function needs to be in an init action I think after the custom post type taxonomy is created. This and my comment above was why I have spent several hours with wp_set_post_terms or wp_set_object_terms not working! Feb 6, 2020 at 14:34
0

as long as the slugs from your articles taxonomy map exactly to the slugs in your default category you should be able to do something like this for each article:

        $article_cats = wp_get_object_terms( $article_id, 'article-category' );
        $new_cats = array();
        foreach( $article_cats AS $t ) {
            $new_cats[] = $t->slug;
        }
        wp_set_object_terms( $post_id, $new_cats, 'categories' );
0

I combined and refined the previous solutions a bit to account for all post statuses (like pending and draft posts for example).

I also made sure to insert the new terms correctly, even if the name and slug are different (encountered something like "Get posts" - "get-pasts" because of a typo in a project).

It also works for custom post types.

function convert_tax($post_type, $old_tax, $new_tax) {
    $pages = get_posts(array(
        'post_type' => $post_type,
        // Get all post statuses
        'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => $old_tax,
                'operator' => 'EXISTS'
            )
        )
    ));

    foreach($pages as $post) {
        $terms = get_the_terms( $post->ID, $old_tax );
        $new_terms = array();

        foreach( $terms as $t ) {
            $new_terms[] = $t->slug;
            
            if( get_term_by( 'slug', $t->slug, $new_tax ) == false ) {
                // Clones the terms into the new taxonomy
                // Added slug parameter to account for name and slug being different
                wp_insert_term( $t->name, $new_tax, $args = array('slug' => $t->slug) );
            }
        }
        
        // Re-sets previously set terms
        wp_set_object_terms( $post->ID, $new_terms, $new_tax );
    }
}

convert_tax('company', 'keywords', 'topics');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.