Skip to content

Instantly share code, notes, and snippets.

@yokotak0527
Last active December 12, 2015 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yokotak0527/4719739 to your computer and use it in GitHub Desktop.
Save yokotak0527/4719739 to your computer and use it in GitHub Desktop.
WordPressで投稿をカスタム投稿に移動させるときのテンプレ
<?php
// WordPress post moves to custom post.
// 参考 -> http://techblog.55w.jp/?p=224
$posts = get_posts(array(
'post_type'=>'post',
'numberposts'=>500
));
// カテゴリーからカスタムタクソノミーに移動させるときに利用
// keyがカテゴリー名でvalがターム名
// key is category name,val is term name.
$old_name_2_new = array(
'カテゴリー名'=>'ターム名',
'カテゴリー名'=>'ターム名',
'カテゴリー名'=>'ターム名',
'カテゴリー名'=>'ターム名'
);
foreach($posts as $post) : setup_postdata($post);
// カスタム投稿 該当施設の設定
$arr = array();
$cat = get_the_category($post->ID);
$old_home_name;
foreach($cat as $num => $obj){
$old_home_name = $obj->name;
if(isset($old_name_2_new[$old_home_name])){
array_push($arr,$old_name_2_new[$old_home_name]);
};
}
wp_set_object_terms($post->ID,$arr,'移動先タームのスラッグ');
//wp_set_object_terms($post->ID,$arr,'term slug of destination.');
// コンテンツの設定
$cnt = get_the_content();
//$cnt = str_replace('','',$cnt);
// ---------------------------------------------------------------------
// 投稿の設定
// ---------------------------------------------------------------------
$my_post=array();
$my_post['ID'] = $post->ID;
//$my_post['post_type'] = ''; // post_type の変更
//$my_post['post_content'] = $cnt; // コンテンツの変更
echo '「'.the_title().'」を変更しました!<br />';
//echo '「'.the_title().'」cahnged!<br />';
echo '----------------------------------------------------------------';
// アップデート
wp_update_post($my_post);
endforeach;
?>
全ての変更が終了しました。
<!-- All changed. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment