WordPress.org

Support

Support » How-To and Troubleshooting » convert all posts in one category to new taxonomy i created

convert all posts in one category to new taxonomy i created

  • so i JUST started to ride the custom taxonomy train, which I think will help clean up my site a bit. the problem is, i’ve been using categories for 3 years now, so all of my posts are already sorted in that fashion. is there a way to move everything over, without redoing all the posts, manually?

    in other words…

    i had a category called “dogs”.
    now i have a taxonomy called “dogs”.

    i want to move all posts in the category over the the taxonomy, then delete the category (and use the taxonomy moving forward). any easy way to do so?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I am also trying to pretty much the same thing – I found this post quite helpful.
    http://phpscrap.com/2011/03/17/wordpress-convert-custom-post-type-into-taxonomycategory/

    This is my first attempt – I didn’t test it thoroughly but it should be enough to get you started if you are a coder. It should create a taxonomy term for each post. It expects the taxonomy to be already existing, and doesn’t delete the categories afterwards. You can put that in functions.php of your theme, load any page or admin screen once, then remove it.

    $gt_taxonomy   = 'project_type';
    $gt_cat   = 49;
    
    //get all posts of category x
    $gt_posts  = query_posts( array( 'post_type'=> 'post', 'cat'=> $gt_cat, 'nopaging' => true ) );
    
    //creaate a new taxonomy for each post, and store the idPost -> idTax map
    if( !empty( $gt_posts ) ){
    	foreach( $gt_posts as  $gt_post ){
    			  wp_set_object_terms( $gt_post->ID, $gt_post->post_name, $gt_taxonomy, false );
    			  $gt_term = get_the_terms( $gt_post->ID, $gt_taxonomy );
    			  $gt_termId  = $gt_term[0]->term_id;
    			  if( $gt_termId ) {
    				wp_update_term( $gt_termId, $gt_taxonomy, array(
    								   'description'    => $gt_post->post_content,
    								   'slug' => $gt_post->post_name,
    								   'name' => $gt_post->post_title
    								   ));
    			  }
    	}
    }

    I have put that in a blog post so that I can change it easily if I find bugs, if anyone’s interested…
    http://blog.gotofritz.net/howto/converting-categories-to-taxonomies-wordpress/

    I have a similar problem from drkknght. Not exactly what fritzthecat is saying.

    I have multiple subcategories in which I’d like create an “upper” level to work around my posts organization, custom taxonomies it is. So I created a parent category (with the custom taxonomy name) to all categories that I want to separate the term I want to convert to taxonomies.

    So far so good, I have the function working but the wp_update_term function doesn’t seem to update the term to a new taxonomy.

    Here is the function:

    migrate_terms(123, 'new-taxonomy');
    function migrate_terms($parent_id, $taxonomy) {
    	$child_terms = get_term_children($parent_id, 'category');
    	foreach($child_terms as $child_term) {
    		$term = get_term_by('id', $child_term, 'category');
    		$args = array(
    			'taxonomy' => $taxonomy
    		);
    		// remove parent because it is now useless
    		if($term->parent == $parent_id)
    			$update = wp_update_term($term->term_id, $term->taxonomy, array('parent' => 0));
    		// update term to new taxonomy
    		$update = wp_update_term($term->term_id, $term->taxonomy, $args);
    	}
    }

    The problem is right on this example:
    wp_update_term(123, 'category', array('taxonomy' => 'new-taxonomy'));

    It returns successfully but doesn’t change the term’s taxonomy. Any ideas?

    Just got it fixed! I’m manually changing taxonomy through $wpdb. Here’s the new function in case anyone needs it:

    migrate_terms(123, 'new-taxonomy');
    function migrate_terms($parent_id, $taxonomy) {
    	global $wpdb;
    	$child_terms = get_terms('category', array('child_of' => $parent_id, 'hide_empty' => 0));
    	if($child_terms) {
    		foreach($child_terms as $child_term) {
    			$args = array(
    				'taxonomy' => $taxonomy
    			);
    			// remove parent because it is now useless
    			if($term->parent == $parent_id)
    				$update = wp_update_term($child_term->term_id, $child_term->taxonomy, array('parent' => 0));
    			// update term to new taxonomy
    			$wpdb->query(
    				"
    				UPDATE $wpdb->term_taxonomy
    				SET taxonomy = '$taxonomy'
    				WHERE term_id = $child_term->term_id
    				"
    			);
    		}
    	} else echo 'No child term was found';
    }

    cheers

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘convert all posts in one category to new taxonomy i created’ is closed to new replies.