Skip to content

Instantly share code, notes, and snippets.

@Strap1
Last active November 26, 2024 09:31

Revisions

  1. Strap1 revised this gist Dec 22, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Convert Custom Taxonomy to Custom Post Type
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,8 @@ function make_posts_from_taxonomy($taxonomy) {
    if(did_action('init') === 1) { //Ensure we only run function once
    // Get all Taxonomy
    $args = array(
    'parent' => 0
    'parent' => 0, //In my case I only wanted top level terms returned
    'hide_empty' => false,
    );

    $taxonomy = 'your_taxonomy'; //Define Custom Taxonomy (source)
  2. Strap1 revised this gist Dec 22, 2013. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Convert Custom Taxonomy to Custom Post Type
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ Version: 0.1
    Author: Strap1
    Author URI: http:/www.hiphopinenglish.com

    /** Convert Taxonomy 'hhie_artists' to CPT 'hhie_artists' **/
    /** Convert Taxonomy '%name%' to CPT '%name%' **/

    function make_posts_from_taxonomy($taxonomy) {
    if(did_action('init') === 1) { //Ensure we only run function once
    @@ -16,8 +16,8 @@ $args = array(
    'parent' => 0
    );

    $taxonomy = 'hhie_artists'; //Define Custom Taxonomy (source)
    $post_type = 'hhie_artists'; // Define Custom Post Type (target)
    $taxonomy = 'your_taxonomy'; //Define Custom Taxonomy (source)
    $post_type = 'your_CPT'; // Define Custom Post Type (target)

    $terms = get_terms( $taxonomy, $args);

    @@ -35,7 +35,7 @@ foreach ($terms as $term) {
    $new_post = array(

    'post_title' => $name,
    'post_content' => $description,
    'post_content' => $description, //Use Taxonomy description for Post Content
    'post_name' => $slug,
    'post_status' => 'publish',
    'post_type' => $post_type,
    @@ -44,7 +44,7 @@ foreach ($terms as $term) {
    //Insert post
    $post_id = wp_insert_post( $new_post );

    //Insert meta where it exists.
    //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;
  3. Strap1 revised this gist Dec 22, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Convert Custom Taxonomy to Custom Post Type
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ $post_type = 'hhie_artists'; // 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
  4. Strap1 revised this gist Dec 22, 2013. 1 changed file with 12 additions and 32 deletions.
    44 changes: 12 additions & 32 deletions Convert Custom Taxonomy to Custom Post Type
    Original file line number Diff line number Diff line change
    @@ -22,38 +22,16 @@ $post_type = 'hhie_artists'; // Define Custom Post Type (target)
    $terms = get_terms( $taxonomy, $args);

    foreach ($terms as $term) {
    $t_id = $term->term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    $t_id = $term->term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    $name = $term->name; //Title
    $slug = $term->slug; //Slug
    $description = $term->description; //Description
    if (!empty($term_meta['buy_download_meta'])) :
    $buy_dl = $term_meta['buy_download_meta'];
    endif;
    if (!empty($term_meta['custom_term_meta'])) :
    $discogs = $term_meta['custom_term_meta'];
    endif;
    if (!empty($term_meta['artist_showcase_meta'])) :
    $showcase = $term_meta['artist_showcase_meta'];
    endif;
    if (!empty($term_meta['itunes_meta'])) :
    $itunes = $term_meta['itunes_meta'];
    endif;
    if (!empty($term_meta['embed_music_meta'])) :
    $embed = $term_meta['embed_music_meta'];
    endif;
    //$discogs = get_option ( "custom_term_meta" ); //Discogs
    //$buy = get_option ( "buy_download_meta" ); //Buy/Download
    //$amazon = get_option ( "amazon_meta" );
    //$itunes = get_option ( "itunes_meta" );
    //$release = get_option ( "release_date_meta" );
    //$showcase = get_option ( "artist_showcase_meta" );
    //$embed = get_option ( "embed_music_meta" );
    //$bandcamp = get_option ( "embed_bandcamp_music_meta" );

    //Above finds all the data from Custom Taxonomy and associated metadata.
    //We make a new post for each item, using same details from Taxonomy
    $new_post = array(
    if( null == get_page_by_title( $name ) ) { // If that post doesn't exist of course.
    $new_post = array(

    'post_title' => $name,
    'post_content' => $description,
    @@ -65,12 +43,14 @@ $term_meta = get_option( "taxonomy_$t_id" );
    //Insert post
    $post_id = wp_insert_post( $new_post );

    //Now we move the metadata if it exists.
    if (!empty($buy_dl)) : update_post_meta ($post_id, '_cmb_buy', $buy_dl); endif;
    if (!empty($discogs)) : update_post_meta ($post_id, '_cmb_discogs', $discogs); endif;
    if (!empty($showcase)) : update_post_meta ($post_id, '_cmb_showcase', $showcase); endif;
    if (!empty($itunes)) : update_post_meta ($post_id, '_cmb_itunes', $itunes); endif;
    if (!empty($embed)) : update_post_meta ($post_id, '_cmb_soundcloud', $embed); endif;
    //Insert meta where it exists.
    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
  5. Strap1 created this gist Dec 20, 2013.
    82 changes: 82 additions & 0 deletions Convert Custom Taxonomy to Custom Post Type
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    <?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 'hhie_artists' to CPT 'hhie_artists' **/

    function make_posts_from_taxonomy($taxonomy) {
    if(did_action('init') === 1) { //Ensure we only run function once
    // Get all Taxonomy
    $args = array(
    'parent' => 0
    );

    $taxonomy = 'hhie_artists'; //Define Custom Taxonomy (source)
    $post_type = 'hhie_artists'; // Define Custom Post Type (target)

    $terms = get_terms( $taxonomy, $args);

    foreach ($terms as $term) {
    $t_id = $term->term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    $name = $term->name; //Title
    $slug = $term->slug; //Slug
    $description = $term->description; //Description
    if (!empty($term_meta['buy_download_meta'])) :
    $buy_dl = $term_meta['buy_download_meta'];
    endif;
    if (!empty($term_meta['custom_term_meta'])) :
    $discogs = $term_meta['custom_term_meta'];
    endif;
    if (!empty($term_meta['artist_showcase_meta'])) :
    $showcase = $term_meta['artist_showcase_meta'];
    endif;
    if (!empty($term_meta['itunes_meta'])) :
    $itunes = $term_meta['itunes_meta'];
    endif;
    if (!empty($term_meta['embed_music_meta'])) :
    $embed = $term_meta['embed_music_meta'];
    endif;
    //$discogs = get_option ( "custom_term_meta" ); //Discogs
    //$buy = get_option ( "buy_download_meta" ); //Buy/Download
    //$amazon = get_option ( "amazon_meta" );
    //$itunes = get_option ( "itunes_meta" );
    //$release = get_option ( "release_date_meta" );
    //$showcase = get_option ( "artist_showcase_meta" );
    //$embed = get_option ( "embed_music_meta" );
    //$bandcamp = get_option ( "embed_bandcamp_music_meta" );

    //Above finds all the data from Custom Taxonomy and associated metadata.
    //We make a new post for each item, using same details from Taxonomy
    $new_post = array(

    'post_title' => $name,
    'post_content' => $description,
    'post_name' => $slug,
    'post_status' => 'publish',
    'post_type' => $post_type,

    );
    //Insert post
    $post_id = wp_insert_post( $new_post );

    //Now we move the metadata if it exists.
    if (!empty($buy_dl)) : update_post_meta ($post_id, '_cmb_buy', $buy_dl); endif;
    if (!empty($discogs)) : update_post_meta ($post_id, '_cmb_discogs', $discogs); endif;
    if (!empty($showcase)) : update_post_meta ($post_id, '_cmb_showcase', $showcase); endif;
    if (!empty($itunes)) : update_post_meta ($post_id, '_cmb_itunes', $itunes); endif;
    if (!empty($embed)) : update_post_meta ($post_id, '_cmb_soundcloud', $embed); endif;


    } //End foreach

    } //End function

    } //Endif did_action
    register_activation_hook( __FILE__, 'make_posts_from_taxonomy' ); //Run on plugin activation
    ?>