I advise you to read:
http://codex.wordpress.org/Function_Reference/add_meta_box http://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
The metabox, save and retrieve options i know it's working:
add_action("add_meta_boxes", "palinsesto_box");
function palinsesto_box() {
add_meta_box("palinsesto-meta", "Speakers", "palinsesto_manager_meta_options", "post", "side", 'high' );
}
function palinsesto_manager_meta_options( $post ) {
wp_nonce_field( 'palinsesto_manager_meta_options', 'palinsesto_manager_meta_options_nonce' );
echo '<label for="speaker_id">';
_e("Speaker", 'speaker_id' );
echo '</label> ';
$args = array( 'post_type' => 'post');
$loop = new WP_Query( $args );
$speaker_id_values = get_post_meta( $post->ID, '_speaker_ids', true ); ?>
<?php if ( $loop->have_posts() ): ?>
<select name="speaker_id[]" id="speaker_id" multiple="multiple">
<?php while( $loop->have_posts() ): $loop->the_post(); ?>
<?php
$selected = ( in_array( get_the_ID(), $speaker_id_values ) ) ? 'selected="selected"' : '';
?>
<option value="<?php the_ID() ?>" <?php echo $selected; ?>><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
<p>Tieni premuto CTRL per selezionare più speakers</p>
<?php endif ?>
<?php
}
add_action('save_post', 'save_palinsesto_manager_meta_options');
function save_palinsesto_manager_meta_options($post_id) {
// Check if our nonce is set.
if ( ! isset( $_POST['palinsesto_manager_meta_options_nonce'] ) )
return $post_id;
$nonce = $_POST['palinsesto_manager_meta_options_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'palinsesto_manager_meta_options' ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
if ( isset( $_POST['speaker_id'] ) ) {
$sanitized_data = array();
$data = (array) $_POST['speaker_id'];
foreach ($data as $key => $value) {
$sanitized_data[ $key ] = (int)strip_tags( stripslashes( $value ) );
}
update_post_meta( $post_id, '_speaker_ids', $sanitized_data );
}
}
But the custom columns I didn't test it.
add_filter( 'manage_edit-speaker_columns', 'set_custom_edit_speaker_columns' );
add_action( 'manage_speaker_posts_custom_column' , 'custom_speaker_column', 10, 2 );
function set_custom_edit_speaker_columns($columns) {
unset( $columns['author'] );
$columns['speaker_id'] = __( 'Speaker', 'your_text_domain' );
return $columns;
}
function custom_speaker_column( $column, $post_id ) {
switch ( $column ) {
case 'speaker_id' :
$speaker_ids = get_post_meta( $post_id, '_speaker_ids', true )
if ( count( $speaker_ids ) > 0 ) {
foreach ( $speaker_ids as $speaker_id ) {
echo get_the_title($speaker_id) . ' ';
}
} else {
echo 'Nothing here';
}
break;
}
}
You can improve the code.