I have the site in WordPress where I used the custom fields, but now I want to use that custom field as post tag automatically.

Can anyone have any idea?


3 Answers 3


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.

 * 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:

register_activation_hook( __FILE__, 'wpse29498_field_to_tag' );
function wpse29498_field_to_tag()
    $posts = get_posts(
            '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.

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

As a plugin.

  • Please is it difficult to imagine the same for converting into a category instead a tag?
    – jjj
    Commented Jun 9, 2015 at 19:55

As far as I am aware, this isn't possible to do automatically "out of the box". A quick Google and it looks like there are currently no plugins that will do the job for you.

However, I can't see any reason why a plugin couldn't be written to do this. You'll need to use the "save_post" hook, which will run every time a post is saved/updated, check the custom field and apply it as a tag.

Documentation on this particular hook is sparse, but it's mentioned here:



I ran into a similar question a while ago from someone asking how to automatically add keywords from the All-in-one SEO plugin as post tags (http://www.vidalquevedo.com/how-to-add-tags/#li-comment-62)

After I saw your question, I took that script I wrote and modified it to automatically add custom field values as post tags as well.

Here's the link to the script and post: http://www.vidalquevedo.com/wordpress-how-to-automatically-convert-custom-fields-to-post-tags/

Thanks, and please let me know if that helps!


  • I think your solution is to add 'All in one seo' keywors as tag as i read on vidalquevedo.com/how-to-add-tags#comment-123 I am not using all in one SEO, simply inserting custom field to post & want that will be the tags for that post. Commented Dec 1, 2011 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.