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!

  • You want a SQL query for achieving this? – Manoj Kumar May 9 '15 at 16:13
  • @ManojKumar - Sure, that would be great! – Mikel May 9 '15 at 16:41
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')

?>
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' );

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.