Created
September 24, 2012 01:47
-
-
Save gatespace/3773780 to your computer and use it in GitHub Desktop.
カスタム投稿タイプAとBを紐づけるメタボックス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* カスタム投稿タイプ「research」の投稿画面に | |
* カスタム投稿タイプ「teacher」の投稿一覧のメタボックスを追加する(チェックボックス編) | |
* teacherの投稿IDをresearchのカスタムフィールド「teacher_ID」に配列で保存 | |
*/ | |
add_action( 'add_meta_boxes', 'add_teacher_list_box' ); | |
function add_teacher_list_box() { | |
add_meta_box('teacher_list', '教員一覧', 'teacher_list_custom_box', 'research', 'side', 'low'); | |
} | |
/* カスタムボックス(teacher_list)のHTML */ | |
function teacher_list_custom_box() { | |
$teacher_ID = get_post_meta( $_GET['post'], 'teacher_ID' ); | |
// 認証に nonce を使う | |
echo '<input type="hidden" name="teacher_list_box_nonce" id="teacher_list_box_nonce" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; | |
// 教員一覧(post_type=teacher)を取得 | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => "teacher", | |
); | |
if ( isset( $post_type['args']->_default_query ) ) | |
$args = array_merge($args, (array) $post_type['args']->_default_query ); | |
$get_posts = new WP_Query; | |
$posts = $get_posts->query( $args ); | |
if ( ! $get_posts->post_count ) { | |
echo '<p>' . __("No items.", 'littleflag') . '</p>'; | |
return; | |
} | |
if ( !$posts ) { | |
$error = '<li id="error">'. $post_type['args']->labels->not_found .'</li>'; | |
} else { | |
echo '<div id="teacher-tab" class="categorydiv">'; | |
echo '<div id="teacher-all" class="tabs-panel">'; | |
echo '<ul class="list:lab categorychecklist form-no-clear" id="teacherchecklist">'; | |
foreach ( $posts as $lf ) : | |
echo '<li class="popular-category" id="teacher-'.$lf->ID.'"><label class="selectit">'; | |
echo '<input type="checkbox" id="in-teacher-'.$lf->ID.'" name="teacher_ID[]" value="'.$lf->ID.'"'; | |
foreach ( $teacher_ID[0] as $rID ) : | |
// var_dump($rID); | |
if( $rID == $lf->ID ) echo ' checked="checked"'; | |
endforeach; | |
echo " /> ".$lf->post_title; | |
echo "</label></li>\n"; | |
endforeach; | |
echo "</ul>\n"; | |
echo "</div>\n"; | |
echo "</div>\n"; | |
} | |
} | |
/* データが入力された際 save_post アクションフックを使って何か行う */ | |
add_action('save_post', 'research_save_postdata'); | |
/* 設定したカスタムフィールドの値をDBに書き込む記述 */ | |
function research_save_postdata( $post_id ) { | |
// 自動保存ルーチンかどうかチェック。そうだった場合はフォームを送信しない(何もしない) | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
// データが先ほど作った編集フォームのから適切な認証とともに送られてきたかどうかを確認。 | |
// save_post は他の時にも起動する場合がある。 | |
if ( !wp_verify_nonce( $_POST['teacher_list_box_nonce'], plugin_basename( __FILE__ ) ) ) | |
return; | |
// パーミッションチェック | |
if ( 'page' == $_POST['post_type'] ) { | |
if ( !current_user_can( 'edit_page', $post_id ) ) | |
return; | |
} else { | |
if ( !current_user_can( 'edit_post', $post_id ) ) | |
return; | |
} | |
// 承認ができたのでデータを探して保存 | |
$mydata = $_POST['teacher_ID']; | |
// $mydata を使って何かを行う | |
// teacher_IDというキーでデータが保存されていなかった場合、新しく保存 | |
if ( "" == get_post_meta( $post_id, 'teacher_ID' )) { | |
add_post_meta( $post_id, 'teacher_ID', $mydata, true ) ; | |
} | |
// teacher_IDというキーのデータと、現在のデータが不一致の場合、更新 | |
elseif ( $mydata != get_post_meta( $post_id, 'teacher_ID' )) { | |
update_post_meta( $post_id, 'teacher_ID', $mydata ) ; | |
} | |
// 現在のデータが無い場合、teacher_IDというキーの値を削除 | |
elseif ( "" == $mydata ) { | |
delete_post_meta( $post_id, 'teacher_ID' ) ; | |
} | |
return $mydata; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment