I created my CPT, but I have some problems:
- when I publish or upgrade the data entered in the field 'ora' they are saved but disappear from the field.
- The permalink does not take its name from the title of the post but from one of the elements of the Metabox 'Conduce' that takes his values from another cpt.
How can I fix it ???
This is my code.
<?php
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' => 'dashicons-format-audio',
'menu_position' => 5,
'supports' => array(
'title',
'thumbnail'
),
);
register_post_type('palinsesto', $args);
}
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
add_image_size( 'cover', 640, 300 );
}
//orario
add_action('add_meta_boxes','palinsesto_box_add');
function palinsesto_box_add(){
add_meta_box('palinsesto_box','Programmazione','program_palinsesto','palinsesto','normal','high');
}
function program_palinsesto($post){
$values = get_post_custom( $post->ID );
$selected = isset( $values['giorno'] ) ? esc_attr( $values['giorno'][0] ) : '';
$text = isset( $values['ora'] ) ? esc_attr( $values['ora'][0] ) : '';
wp_nonce_field(__FILE__, 'palinsesto_nonce');
?>
<p>
<label for="giorno"><b>Giorno</b></label>
<select name="giorno" id="giorno">
<option value="lunedi" <?php selected( $selected, 'lunedi' ); ?>>Lunedi</option>
<option value="martedi" <?php selected( $selected, 'martedi' ); ?>>Martedi</option>
<option value="mercoledi" <?php selected( $selected, 'mercoledi' ); ?>>Mercoledi</option>
<option value="giovedi" <?php selected( $selected, 'giovedi' ); ?>>Giovedi</option>
<option value="venerdi" <?php selected( $selected, 'venerdi' ); ?>>Venerdi</option>
<option value="sabato" <?php selected( $selected, 'sabato' ); ?>>Sabato</option>
<option value="domenica" <?php selected( $selected, 'domenica' ); ?>>Domenica</option>
</select>
</p>
<p>
<label for="ora"> <b>Ora (formato hh.mm)</b></label>
<input type="text" name="ora" id="ora" value="<?php echo $text; ?>" />
</p>
<p>
</p>
<?php
}
//fine orario
add_action("add_meta_boxes", "palinsesto_box");
function palinsesto_box() {
add_meta_box("palinsesto-meta", "Conduce", "palinsesto_manager_meta_options", "palinsesto", "side" );
}
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' => 'speaker');
$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" required>
<?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 i conduttori</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 );
}
}
//save
add_action('save_post','palinsesto_box_save');
function palinsesto_box_save($post_id){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ($_POST && wp_verify_nonce($_POST['palinsesto_nonce'], __FILE__) ) {
if( isset( $_POST['giorno'] ) )
update_post_meta( $post_id, 'giorno', esc_attr( $_POST['giorno'] ) );
if( isset( $_POST['ora'] ) )
update_post_meta( $post_id, 'ora', esc_attr( $_POST['ora'] ) );
}}