Memolog
新しい投稿を既存の記事のどれかに紐付けたい時とか、なんかどっかで使いたかった気がするのでメモ。
/* 投稿ページに表示させるカスタムボックスの設定 */
add_action('admin_menu', 'allpost_inputbox');
// 投稿ページ(post)に表示させる「全投稿選択欄」の設定
function allpost_inputbox() {
add_meta_box('allpost_div', '全投稿選択欄', 'allpost_field', 'post');
}
// 投稿ページに表示させるカスタムフィールドの設定
function allpost_field(){
$id = get_the_ID();
$allpost_select = get_post_meta($id, 'allpost_select', true);
// 投稿データ(post)の取得
$allpost = get_posts('post_type=post&posts_per_page=-1&post_status=publish,draft,private');
wp_nonce_field(wp_create_nonce(), 'allpost_nonce');
echo '投稿をお選びください';
echo '<select name="allpost_select">';
foreach($allpost as $onepost){
$selected = ((int)$allpost_select === $onepost->ID)?' selected="selected"':'';
echo '<option value="'.$onepost->ID.'"'.$selected.'>'.$onepost->post_title.'</option>';
}
echo '</select>';
wp_reset_postdata();
}
/* 入力したデータの更新処理の設定 */
add_action('save_post', 'allpost_save_postdata');
// カスタムフィールドの更新/保存の設定
function allpost_save_postdata($post_id){
$allpost_select = isset($_POST['allpost_select'])?$_POST['allpost_select']:'';
update_post_meta($post_id, 'allpost_select', $allpost_select);
}
コメントを投稿する