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.


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;

    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.

  • 1
    Do you just want to do this once for all your current posts? Or every time a post is added/edited? Or both? – ngearing Sep 20 '16 at 5:41
  • I want to change all for the existing articles – pradip Sep 20 '16 at 7:28
  • Yes once, for all the existing articles ! New articles i can add manually... – pradip Sep 20 '16 at 7:38

Since this is a one-time task, it's easiest to create a migration script like so.

Note: It might timeout (trying to process 90k rows) depending on how much memory your server has.

function my_migration_script() {
  global $wpdb;

  // Quick and dirty way to get post ids. Normally don't use this method.
  $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'post'" );

  // Loop through all ids.
  foreach ( $post_ids as $post_id ) {

    // Grab the location meta value if it exists.
    if ( $value = get_post_meta( $post_id, 'location', true ) ) {

      // Remove any commas since it's used as a delimiter.
      $value = str_replace ( ',', '', $value );

      // Create a tag based on the `location` custom field value.
      wp_add_post_tags( $post_id, $value );

  // Uncomment if you'd rather delete without verifying first.
  // $wpdb->delete( $wpdb->postmeta, array( 'meta_key' => 'location' ) );

// Kick off the script.

Lastly, delete all the old location custom field values directly from the db (using phpMyAdmin or whatever db access tool you have).

DELETE FROM wp_postmeta WHERE `meta_key` = 'location';

I put this outside of the script so you can first verify the migration was successful, and to save php memory resources during the execution.

If you don't have phpMyAdmin access, just uncomment the line above in the script and run it again.

Note: If the tag already exists, it won't duplicate it so you can run the script multiple times without harm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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