如何将Post对象自定义域转换为标签

时间:2019-07-25 作者:chris

因此,我在本文中使用了这个脚本来自动将现有的自定义字段转换为标记。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" 

1 个回复
最合适的回答,由SO网友:Karun 整理而成

假设$value 实际返回帖子的ID。替换代码

wp_add_post_tags( $post_id, $value );
使用

wp_add_post_tags( $post_id, sanitize_title( get_the_title( $value[0] ) ) );

相关推荐

getting posts by tags

我使用下面的代码通过单词标签获取帖子$search_songs = array( \'posts_per_page\' => -1, \'post_type\' => \'songs\', \'tax_query\' => array ( array ( \'taxonomy\' => \'post_tag\',