The code you need to use here has some steps. First you need to create e metabox on your custom post type edit.php which will list all your posts of test_chapter post type. Then you have to hook the save_post action so you set the parent of your edited post to the chosen "test_chapter". The best way is to connect these two post types as post meta and not as parent-child relation so you can handle it better. According to this tutorial: 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
Have not tested the code so it may have some mistakes. Let me knwo so I can correct them.