phpwordpresssavecustom-post-type

wordpress save multiple value from select item


i've created a meta box with a multiple select item that takes its values from a custom post type.

add_action("add_meta_boxes", "palinsesto_box");

function palinsesto_box() {
add_meta_box("palinsesto-meta", "Speakers",   "palinsesto_manager_meta_options", "palinsesto",   "side");}

function palinsesto_manager_meta_options($post)
{
wp_nonce_field( 'radio_schedule', 'schedule_noncename' );
echo '<label for="speaker_id">';
_e("Speaker", 'speaker_id' );
echo '</label> ';
$args = array( 'post_type' => 'speaker');
$loop = new WP_Query( $args );
echo '<select name="speaker_id[]" id="speaker_id" multiple="multiple">';
foreach($loop->posts as $dj):
    if($dj->ID == get_post_meta( $post->ID, 'speaker_id', true ))
    {
        $select = 'selected';
    }else{
    $select = '';
}
echo '<option value="'.$dj->ID.'" '.$select.'>'.$dj->post_title.'</option>';
endforeach;
echo '</select>';
echo '<p>Tieni premuto CTRL per selezionare più speakers</p>';
}

this part works!Now i would save the selected values and display it in a custom column and here there is something worng.

add_action('save_post', 'save_palinsesto_manager_meta_options');
function save_palinsesto_manager_meta_options($post_id)
{global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
    //if you remove this the sky will fall on your head.
    return;
}else{
    if( isset( $_POST['speaker_id'] ) ) {  
    update_post_meta( $post_id,'speaker_id', esc_attr( $_POST['speaker_id'] ) );  
}
}
}

add_filter('manage_palinsesto_posts_columns', 'columns_palinsesto');
function columns_palinsesto($old_columns)
{
$new_columns = array(
    'cb'     => '<input type="checkbox">',
    'img'    => 'Immagine',
    'title'  => __('Palinsesto'),
    'conduce' => 'Conduce',

);
return array_merge( $new_columns, $old_columns );
}

add_action('manage_palinsesto_posts_custom_column', 'get_palinsesto_columns',
10, 2);
function get_palinsesto_columns($col, $post_id)
{global $post;

$conduce=get_post_meta( $post_id,'speaker_id',$value );


switch($col) {
case 'img':
    if(has_post_thumbnail($post_id)) {
        echo get_the_post_thumbnail($post_id);
    } else {
        echo 'Nessuna immagine!';
    }
    break;
case 'conduce':

    echo $conduce;
    break;
}
}

This part of code only returns one value and displays the word array or numbers....can u help me????


Solution

  • 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.