1
votes

I made a metabox with a multiple selction item that takes its values from another custom post and i would save a multiple selection and display it in the admin page. Whit this code i almost did it but: the multiple select item and the save function only save one value and the columns function only display the word Array or the id....can you help me.

this is the code

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>';
}

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', true );


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 is the meta box part....

add_action('init', 'palinsesto_manager');
function palinsesto_manager() {

$labels = array(
    'name'               => __('Palinsesto'),
    'singular_name'      => __('programma'),
    'add_new'            => __('Aggiungi Programma'),
    'add_new_item'       => __('Nuovo Programma'),
    'edit_item'          => __('Modifica Programma'),
    'new_item'           => __('Nuovo Programma'),
    'all_items'          => __('Palinsesto'),
    'view_item'          => __('Visualizza '),
    'search_items'       => __('Cerca '),
    'not_found'          => __('Programma non trovato'),
    'not_found_in_trash' => __('Programma non trovato nel cestino'),
);

$args = array(
    'labels'             => $labels,
    'public'             => true,
    'show_ui'    => true,
    'rewrite'            => array('slug' => 'palinsesto'),
    'publicly_queryable' => true,
    'has_archive'        => true,
    'capability_type' => 'post',
    'hierarchical'       => false,
    'menu_icon' => get_stylesheet_directory_uri() . '/images/palinsestoic.png',
    'menu_position'      => 5,
    'supports'           => array(
        'title',
        'thumbnail'
    ),
);

register_post_type('palinsesto', $args);
}

if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150, true );
add_image_size( 'cover', 260, 160 );
}
add_action("add_meta_boxes", "palinsesto_box");

function palinsesto_box() {
add_meta_box("palinsesto-meta", "Speakers",   "palinsesto_manager_meta_options",   "palinsesto",   "side");}
1
You posted the wrong code. The columns part is irrelevant, and the meta box part is missing. Hint: indenting your code makes life easier. Like this is unreadable. For me no problem because I paste in my editor and select "auto indent" and presto: readable code. - brasofilo
the metabox is working,the select item too (that takes its value from another custom post)the only problem is that it saves only one value and the admin displays the word array or the id number - zen
Impossible to help without proper code to reproduce the issue, please see: sscce.org - brasofilo
@b__ is this the missing code??? - zen

1 Answers

5
votes

change name attribute from speaker_id to speaker_id[]

change this code:

<select name="speaker_id" id="speaker_id" multiple="multiple">

To

<select name="speaker_id[]" id="speaker_id" multiple="multiple">

and it will be saved as array