这里需要使用的代码有一些步骤。首先,您需要在自定义帖子类型编辑上创建e元数据库。php,它将列出您所有的test\\u chapter post类型的帖子。然后,您必须挂接save\\u post操作,以便将已编辑帖子的父级设置为所选的“test\\u chapter”。最好的方法是将这两种帖子类型连接为post meta,而不是父子关系,以便更好地处理它。根据本教程:http://wptheming.com/2010/08/custom-metabox-for-post-type/
add_action( \'add_meta_boxes\', \'add_books_metaboxes\' );
// Add the Book Meta Boxes
function add_books_metaboxes() {
add_meta_box(\'wpt_test_chapter\', \'Book chapter\', \'wpt_test_chapter\', \'test_book\', \'side\', \'default\');
}
// Book chapter Metabox
function wpt_test_chapter() {
global $post;
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'test_chapter\',
);
$books_array = get_posts( $args );
$chapter_ids = get_post_meta($post->ID, \'related_chapters\', true);
?>
<select name="book_chapters" multiple>
<?php
foreach($books_array as $book_array){
if(in_array($book_array->ID, $chapter_ids)){$selected = \'selected;\'}
else{$selected = \'\';}
echo \'<option \'.$book_array->ID.\' \'.$selected.\'>\'.$book_array->post_title.\'</option>\';
}
?>
</select>
<?php
// Noncename needed to verify where the data originated
/* if you are using plugin:
echo \'<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="\' .
wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
*/
}
// Save the Metabox Data
function wpt_save_chapter_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
/* if you are using plugin:
if ( !wp_verify_nonce( $_POST[\'eventmeta_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
*/
// Is the user allowed to edit the post or page?
if ( !current_user_can( \'edit_post\', $post->ID ))
return $post->ID;
// OK, we\'re authenticated: we need to find and save the data
// We\'ll put it into an array to make it easier to loop though.
$chapters_ids[\'book_chapters\'] = $_POST[\'book_chapters\'];
// Add values of $events_meta as custom fields
foreach ($chapter_ids as $chapter_id) { // Cycle through the $events_meta array!
//if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice
update_post_meta($post->ID, \'related_chapters\', $chapter_id);
}
}
add_action(\'save_post\', \'wpt_save_chapter_meta\', 1, 2); // save the custom fields
没有测试代码,因此可能会有一些错误。让我知道,这样我可以纠正他们。