WORDPREE循环添加标签库存在的标签

放在根目录下!!!~~然后挨个分类刷新下就成了

<?php   
      include ( "wp-config.php" ) ; 
      require_once (ABSPATH.'wp-blog-header.php'); 

      global $wpdb;  
  
      $CID = 2;//分类id,只支持一个分类

      $sql="SELECT ID,post_title,post_content FROM wp_posts,wp_term_relationships,wp_term_taxonomy WHERE ID=object_id and wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id and post_type='post' and post_status = 'publish' and wp_term_relationships.term_taxonomy_id = $CID and taxonomy = 'category' order by ID desc"; 
      $myrows = $wpdb->get_results($sql);
 	  add_action('save_post', 'auto_add_tags');
      foreach ($myrows as $data) {
      echo $data->ID."<br />";//这是文章ID
      echo $data->post_title."<br />";//这是文章标题
      //echo $b->post_content."<br />";//这是文章内容
	  
	    $tags = get_tags( array('hide_empty' => false) );
        $post_id = $data->ID;
        $post_content = get_post($data->ID)->post_content;
        if ($tags) {
                $i = 0;
                $arrs = object2array($tags);
                shuffle($arrs);
                $tags = array2object($arrs);
                // 打乱顺序
                foreach ( $tags as $tag ) {
                        // 如果文章内容出现了已使用过的标签,自动添加这些标签
                        if ( strpos($post_content, $tag->name) !== false){
                                if ($i == 15) {
                                        // 控制输出数量
                                        break;
                                }
                                wp_set_post_tags( $post_id, $tag->name, true );
                                $i++;
                        }
                }
        }
      }
?>