LoginSignup
1

More than 5 years have passed since last update.

Wordpressのタグ削除ではなく、記事とタグの関係を「切断」する方法

Posted at

WordPressのタグ管理で、使用したくないタグを消去しても、自動タグ付与(simple_tagsのプラグイン等)を利用していると、再度タグが付けられて、なおかつIDが別で振られることになります。

そこで、タグのIDは削除せず、使用されている記事とタグの関係を「切断」する形で使用すれば、いいかもしれません。(定期的なCRONなどで自動実行形式に)

実際私のところのサイト運用では、あるタグ(例「www」、IDは120、件数は30)に関連づく、各記事のタグを消去するようなツールをCRONで定期的に稼働させて消去させています。

ソースコードは醜いですが、以下のような感じです。

syori.php
<?php

// WordPressのインストール先の「wp-load.php」を読み込む
require dirname(__FILE__) . "/wp-load.php";

// 削除(消去)するキーワード一覧のファイル名
$filename = dirname(__FILE__) . "/keywords.ini";

// キーワード一覧を読み込む
$term_name = file_get_contents($filename);

// 改行ごとで配列化
$term_name2 = explode("\n",$term_name);
// 配列の件数
$cnt = count($term_name2);

$str = array();

// 一度に処理する件数(単語の件数)
$scope_count = 20;
for($i=0;$i < $cnt; $i++){
    if( ($i % $scope_count == 0 ) && ( $i != 0 ) ){
        $term_name3[] = $str;
        $str = array();
    }
    $str[] = $term_name2[$i];
}

// print_r($term_name3);


// =========

// 読み込む
foreach($term_name3 as $terms){

    // 配列から件数分を「カンマ区切り」にする
    $term_name = implode(",",$terms);
    echo "term_name[" . $term_name . "]\n";

    // query_postsに突っ込むキーワードはカンマ区切りが必須
    // キーワードが使われいる記事を最大500件取得
    query_posts('posts_per_page=500&tag=' . $term_name );

    if (have_posts()) :
        while (have_posts()) : the_post();

// echo get_the_title();
//        echo '<a href="' . get_permalink() . '">' . get_the_title() . '/' . get_the_ID() . '</a><br />' . "\n";

        echo "    " .get_the_title() . '/' . get_the_ID() . "\n";
        // 当該記事に関連付けられているキーワードを配列で渡し、削除(消去)する
        wp_remove_object_terms(get_the_ID(), $terms,"post_tag");

        endwhile;
    endif;

    wp_reset_query();
}

?>

keyword.ini
http
https
www
com
co
posts
jp
facebook

お役に立てれば。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1