I have a site with a custom field (location) on every post . I want to change the value of each custom field value in every article to tag.
I tried searching here but couldn’t get much info .
Some useful links here:
Plugin to auto convert custom fields to tag
I’ve tried using the following code but none of it works.
add_action('save_post','custom_field_add_tags');
function custom_field_add_tags($post_id) {
$post = get_post($post_id);
//get values of custom fields and put into array
$tag1 = get_post_meta($post_id, 'key_1', true);
$tag2 = get_post_meta($post_id, 'key_2', true);
$tag3 = get_post_meta($post_id, 'key_3', true);
$tags_to_add = array($tag1, $tag2, $tag3);
//now check if tag does not already exist (if no - add tag from custom field)
$add_tags = array();
foreach(get_the_terms($post_id, 'post_tag') as $term) {
if(!in_array($term->slug, $tags_to_add))
$add_tags[] = $term->slug;
}
if(!empty($add_tags))
wp_add_post_tags($post_id, implode(',', $add_tags));
}
Another :
global $wpdb;
$sql = "SELECT post_id , meta_value FROM ".$wpdb->prefix."postmeta WHERE meta_key = 'cp_additional_options' ";
$res = $wpdb->get_results( $sql ) ;
if( !empty($res)){
foreach( $res as $r ){
$ret = wp_insert_term( $r->meta_value , 'pa_popular-features' );
if( !$ret->errors ){
$term_id = $ret['term_id'];
wp_set_post_terms( $r->post_id, $term_id, 'pa_popular-features');
}
}
}
The no of articles on my site is huge .. more than 90,000 articles.
Can this be done with a simple function or through MySQL command.