Plugin to auto convert custom fields to tag

Placing something like this in your functions.php should work. I haven’t had the opportunity to test this code yet and its a little messy (could be polished up) but should give you an very good starting point..

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));
}

Leave a Comment