amritansh of Tact9-GIST
4/1/2016 - 1:13 PM

This function converts meta of a post into the term

This function converts meta of a post into the term

/*
 * This function converts meta of a post into the term
 */
convert_metato_term($posttype, $taxanomy_name, $meta_key_name);

function convert_metato_term($posttype, $taxanomy_name, $meta_key_name) {
    global $post;
    $args = array(
        'post_type' => $posttype,
        'post_status' => 'publish',
        'posts_per_page' => -1,
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
            $meta_value = get_post_meta($post->ID, $meta_key_name, true);
            $term_id = term_exists($meta_value, $taxanomy_name);
            if ($term_id !== 0 && $term_id !== null) {
                $term_id = $term_id['term_id'];
            } else {
                $term_id = wp_insert_term($meta_value, $taxanomy_name);
                $term_id = $term_id['term_id'];
            }
        endwhile;
    endif;
}