Skip to content

Instantly share code, notes, and snippets.

@Strap1
Last active July 24, 2024 20:27
Show Gist options
  • Save Strap1/8053824 to your computer and use it in GitHub Desktop.
Save Strap1/8053824 to your computer and use it in GitHub Desktop.
A very hacky and quick way to transfer a Custom Taxonomy to Custom Post Type and transfer associated metadata to Custom Meta Fields. Note: You can use this if it fits your needs, but it is custom to my set up. Use in a testing environment. It's a plugin with no interface, runs on activation and depending on the amount of data, may hit PHP timeou…
<?php
/*
Plugin Name: Convert Custom Taxonomy to Custom Post Type
Plugin URI: N/A
Description: A plugin to convert a Custom Taxonomy to a Custom Post Type and transfer associated metadata.
Version: 0.1
Author: Strap1
Author URI: http:/www.hiphopinenglish.com
/** Convert Taxonomy '%name%' to CPT '%name%' **/
function make_posts_from_taxonomy($taxonomy) {
if(did_action('init') === 1) { //Ensure we only run function once
// Get all Taxonomy
$args = array(
'parent' => 0, //In my case I only wanted top level terms returned
'hide_empty' => false,
);
$taxonomy = 'your_taxonomy'; //Define Custom Taxonomy (source)
$post_type = 'your_CPT'; // Define Custom Post Type (target)
$terms = get_terms( $taxonomy, $args);
foreach ($terms as $term) {
set_time_limit(20); //Attempt to prevent timeouts
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$name = $term->name; //Title
$slug = $term->slug; //Slug
$description = $term->description; //Description
//Above finds all the data from Custom Taxonomy and associated metadata.
//We make a new post for each item, using same details from Taxonomy
if( null == get_page_by_title( $name ) ) { // If that post doesn't exist of course.
$new_post = array(
'post_title' => $name,
'post_content' => $description, //Use Taxonomy description for Post Content
'post_name' => $slug,
'post_status' => 'publish',
'post_type' => $post_type,
);
//Insert post
$post_id = wp_insert_post( $new_post );
//Insert meta where it exists. Note that my meta is stored like so: http://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/
if (!empty($term_meta['buy_download_meta'])) : update_post_meta ($post_id, '_cmb_buy', $term_meta['buy_download_meta']); endif;
if (!empty($term_meta['custom_term_meta'])) : update_post_meta ($post_id, '_cmb_discogs', $term_meta['custom_term_meta']); endif;
if (!empty($term_meta['itunes_meta'])) : update_post_meta ($post_id, '_cmb_itunes', $term_meta['itunes_meta']); endif;
if (!empty($term_meta['artist_showcase_meta'])) : update_post_meta ($post_id, '_cmb_showcase', $term_meta['artist_showcase_meta']); endif;
} else { // Do sweet F.A.
}
} //End foreach
} //End function
} //Endif did_action
register_activation_hook( __FILE__, 'make_posts_from_taxonomy' ); //Run on plugin activation
?>
@n1k1c4
Copy link

n1k1c4 commented Nov 11, 2016

Awesome! Thank you!

@dougkeeling
Copy link

Thanks for this. Still helpful several years on. 💯

@mobleyt
Copy link

mobleyt commented Apr 1, 2019

Thanks for this. Saved me a ton of time getting an old WP site refactored.

@kevisorus
Copy link

Many thanks. This saved me a lot of time.

@Strap1
Copy link
Author

Strap1 commented Apr 21, 2021

Many thanks. This saved me a lot of time.

Glad to hear it.
Forgot all about this. Glad to hear folks are still finding this useful after a long time.

@Tetrakern
Copy link

Possible I miss something here, but the line $term_meta = get_option( "taxonomy_$t_id" ); will not work (at least it did not when I tested it), and neither will the subsequent lines to insert meta fields. Term meta is not an option, you need to query them piece by piece via get_term_meta( $term_id, $key, true ) or alternatively a custom SQL query to get all at once as array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment