0

so I've used this script in from this post to automatically convert an existing custom field into a tag. The problem is that my custom field is a Post Object. SO, it is creating a tag with the post ID. Can you advise on how to tag as the actual post title?

How to convert custom field value to tag

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.
my_migration_script();

Updated w/ var_dump values:

array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10362" } array(1) { [0]=> string(5) "13239" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13236" } array(2) { [0]=> string(5) "13236" [1]=> string(5) "13889" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(2) { [0]=> string(5) "13236" [1]=> string(5) "13889" } array(1) { [0]=> string(5) "13889" } array(1) { [0]=> string(5) "10346" } array(1) { [0]=> string(5) "13889" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13889" } array(2) { [0]=> string(5) "13889" [1]=> string(5) "13236" } array(2) { [0]=> string(5) "13234" [1]=> string(5) "13889" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "10346" } array(2) { [0]=> string(5) "13236" [1]=> string(5) "13889" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "14060" } array(1) { [0]=> string(5) "13889" } array(2) { [0]=> string(5) "13889" [1]=> string(5) "13236" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13236" } array(2) { [0]=> string(5) "13889" [1]=> string(5) "13236" } string(5) "13234" array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "14060" } array(1) { [0]=> string(5) "14060" } array(2) { [0]=> string(5) "13236" [1]=> string(5) "13889" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13889" } array(1) { [0]=> string(5) "10366" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "13452" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "13889" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13889" } array(1) { [0]=> string(5) "14798" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "14680" } array(1) { [0]=> string(5) "13889" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13234" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(2) "87" } array(1) { [0]=> string(5) "13610" } array(1) { [0]=> string(5) "13236" } array(1) { [0]=> string(5) "14680" } array(1) { [0]=> string(5) "10345" } array(1) { [0]=> string(5) "13889" 
0

Assuming the $value actually returns a post's ID. Replace the code

wp_add_post_tags( $post_id, $value );

With

wp_add_post_tags( $post_id, sanitize_title( get_the_title( $value[0] ) ) );
9
  • Thanks for that! It makes sense, but for some reason, it did not seem to work. Any ideas? – chris Jul 25 '19 at 14:57
  • What's the output of your $value when you var_dump? – Karun Jul 25 '19 at 14:59
  • I'm not quite sure how to do that. But, when I executed the script the first time, it was returning the Post ID properly. I referenced a few tags and they seemed to be tagged with the correct Post ID. – chris Jul 25 '19 at 15:00
  • Do a var_dump( $value ) before your wp_add_post_tags code. If you are sure the $value will return a post ID, the above code should actually work. – Karun Jul 25 '19 at 15:03
  • OK, I'll try that now. Where do I see the output of the var_dump? Sorry for my ignorance! – chris Jul 25 '19 at 15:05

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.