To convert the old custom fields, you can write a plugin that, on activation, fetches all the posts with your custom field name and converts them to tags with the handy wp_set_post_terms
.
First set up a few constants with your field names and post type.
<?php
/**
* CHANGE THIS! what is your custom field's name?
*/
define( 'WPSE29498_FIELD', 'custom_field_name' );
/**
* CHANGE THIS! what post type?
*/
define( 'WPSE29498_TYPE', 'post' );
Then the activation hook function:
<?php
register_activation_hook( __FILE__, 'wpse29498_field_to_tag' );
function wpse29498_field_to_tag()
{
$posts = get_posts(
array(
'post_type' => WPSE29498_TYPE,
'post_status' => array( 'publish', 'draft', 'pending', 'future' ),
'numberposts' => -1,
'meta_key' => WPSE29498_FIELD
)
);
if( empty( $posts ) ) return;
foreach( $posts as $p )
{
if( $meta = get_post_meta( $p->ID, WPSE29498_FIELD, true ) )
{
wp_set_post_terms( $p->ID, $meta, 'post_tag', true );
}
}
}
And finally, you can hook into save_post
and see if the custom field value is there. If so, make sure it’s not a term already, then insert it if it isn’t.
Not sure if this is something you needed or a one time conversion.
<?php
add_action( 'save_post', 'wpse29498_save_post' );
function wpse29498_save_post( $post_id )
{
if( $meta = get_post_meta( $post_id, WPSE29498_FIELD, true ) )
{
// get the current post tag
$terms = wp_get_object_terms( $p->ID, 'post_tag', 'name' );
// if our term is already there, bail
if( in_array( $meta, $terms ) ) return;
// add the term if not
wp_set_post_terms( $post_id, $meta, 'post_tag', true );
}
}