1

I am able to make custom post type and custom meta box but my situation is that i want to attach multiple instance of custom meta box to custom post type.

my custom post type is show which have custom fields like start date,link,venue lets call it showinfo

now for every show i can have multiple showinfo

so how to achieve that

in attached image see show events time box in right middle side of picture that better explain what i am asking check attached image

here is my code

function uep_custom_post_type() {
$labels = array(
    'name'                  =>   __( 'Shows', 'uep' ),
    'singular_name'         =>   __( 'Show', 'uep' ),
    'add_new_item'          =>   __( 'Add New Show', 'uep' ),
    'all_items'             =>   __( 'All Shows', 'uep' ),
    'edit_item'             =>   __( 'Edit Show', 'uep' ),
    'new_item'              =>   __( 'New Show', 'uep' ),
    'view_item'             =>   __( 'View Show', 'uep' ),
    'not_found'             =>   __( 'No Show Found', 'uep' ),
    'not_found_in_trash'    =>   __( 'No Show Found in Trash', 'uep' )
);

$supports = array(
    'title',
    'editor',
    'excerpt'
);

$args = array(
    'label'         =>   __( 'Shows', 'uep' ),
    'labels'        =>   $labels,
    'description'   =>   __( 'A list of upcoming shows', 'uep' ),
    'public'        =>   true,
    'show_in_menu'  =>   true,
    'show_in_nav_menus' =>true,
    'menu_icon'     =>   '',
    'has_archive'   =>   true,
   // 'rewrite'       =>   true,
    'supports'      =>   $supports,
    'has_archive' => true
   //'taxonomies' => array('category', 'post_tag') 
);

register_post_type( 'show', $args );
}
add_action( 'init', 'uep_custom_post_type' );

function uep_render_event_info_metabox( $post ) {

global $post;
// generate a nonce field
wp_nonce_field( basename( __FILE__ ), 'uep-event-info-nonce' );

// get previously saved meta values (if any)
$event_start_date = get_post_meta( $post->ID, 'event-start-date', true );
$event_end_date = get_post_meta( $post->ID, 'event-end-date', true );
$event_venue = get_post_meta( $post->ID, 'event-venue', true );

// if there is previously saved value then retrieve it, else set it to the current time
$event_start_date = ! empty( $event_start_date ) ? $event_start_date : time();

//we assume that if the end date is not present, event ends on the same day
$event_end_date = ! empty( $event_end_date ) ? $event_end_date : $event_start_date;

$venues=get_terms("tf_evenvenue",'hide_empty=0');


$sdate=get_post_meta( $post->ID, 'event-start-date', true);
if($sdate!="")
  $sdate=date("m/d/Y",$sdate);
$edate=get_post_meta( $post->ID, 'event-end-date', true);
if($edate!="")
  $edate=date("m/d/Y",$edate);
$v=get_post_meta($post->ID,"event-venue",true);
$tlink=get_post_meta($post->ID,"event-ticket-link",true);


?>

<label for="uep-event-start-date"><?php _e( 'Show Start Date:', 'uep' ); ?></label>
<input class="widefat uep-event-date-input" id="uep-event-start-date" type="text"      name="uep-event-start-date" placeholder="Format: February 18, 2014" value="<?php echo  $sdate;?>"   />

<label for="uep-event-end-date"><?php _e( 'Show End Date:', 'uep' ); ?></label>
<input class="widefat uep-event-date-input" id="uep-event-end-date" type="text" name="uep-event-end-date" placeholder="Format: February 18, 2014"  value="<?php echo $edate;?>" />

<label for="uep-event-link"><?php _e( 'Ticket Link:', 'uep' ); ?></label>
<input class="widefat" id="uep-event-ticket-link" type="text" name="uep-event-ticket-link"  value="<?php echo $tlink;?>" />

<label for="uep-event-venue"><?php _e( 'Venue:', 'uep' ); ?></label>
<select class="widefat" id="uep-event-venue" name="uep-event-venue">
<?php
foreach ($venues as $venue) {
$s="";
if(strlen($v)>0 && strcmp($venue->slug,$v)==0)
  $s=" SELECTED";
?>
<option value="<?php echo $venue->slug;?>" <?php echo $s;?> ><?php echo $venue->name;  ?></option>

<?php }
?>
</select>


<?php  
}


function uep_add_event_info_metabox() {
add_meta_box(
    'uep-event-info-metabox',
    __( 'Show Info', 'uep' ),
    'uep_render_event_info_metabox',
    'show',
    'side',
    'high'
);
}
add_action( 'add_meta_boxes', 'uep_add_event_info_metabox' );

function uep_admin_script_style( $hook ) {
global $post_type;

if ( ( 'post.php' == $hook || 'post-new.php' == $hook ) && ( 'show' == $post_type ) ) {
    wp_enqueue_script(
        'upcoming-events',
        SCRIPTS . 'script.js',
        array( 'jquery', 'jquery-ui-datepicker' ),
        '1.0',
        true
    );

    wp_enqueue_style(
        'jquery-ui-calendar',
        STYLES . 'jquery-ui-1.10.4.custom.min.css',
        false,
        '1.10.4',
        'all'
    );
}
}
add_action( 'admin_enqueue_scripts', 'uep_admin_script_style' );


function uep_save_event_info( $post_id ) {

 // checking if the post being saved is an 'event',
// if not, then return
if ( 'show' != $_POST['post_type'] ) {
    return;
}


// checking for the 'save' status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST['uep-event-info-nonce'] ) && ( wp_verify_nonce( $_POST['uep-event-info-nonce'], basename( __FILE__ ) ) ) ) ? true : false;

// exit depending on the save status or if the nonce is not valid
if ( $is_autosave || $is_revision || ! $is_valid_nonce ) {
    return;
}

// checking for the values and performing necessary actions
if ( isset( $_POST['uep-event-start-date'] ) ) {
    update_post_meta( $post_id, 'event-start-date', strtotime( $_POST['uep-event-start-date'] ) );
}

if ( isset( $_POST['uep-event-end-date'] ) ) {
    update_post_meta( $post_id, 'event-end-date', strtotime( $_POST['uep-event-end-date'] ) );
}

if ( isset( $_POST['uep-event-venue'] ) ) {
    update_post_meta( $post_id, 'event-venue', sanitize_text_field( $_POST['uep-event-venue'] ) );
}

  if ( isset( $_POST['uep-event-ticket-link'] ) ) {
    update_post_meta( $post_id, 'event-ticket-link', sanitize_text_field( $_POST['uep-event-ticket-link'] ) );
}
}
add_action( 'save_post', 'uep_save_event_info' );

function uep_custom_columns_head( $defaults ) {
unset( $defaults['date'] );

$defaults['event_start_date'] = __( 'Start Date', 'uep' );
$defaults['event_end_date'] = __( 'End Date', 'uep' );
$defaults['event_venue'] = __( 'Venue', 'uep' );

return $defaults;
}
add_filter( 'manage_edit-event_columns', 'uep_custom_columns_head', 10 );

function uep_custom_columns_content( $column_name, $post_id ) {

//echo $column_name . "<hr>";
if ( 'event-start-date' == $column_name ) {
    $start_date = get_post_meta( $post_id, 'event-start-date', true );
    echo date( 'F d, Y', $start_date );
}

if ( 'event_end_date' == $column_name ) {
    $end_date = get_post_meta( $post_id, 'event-end-date', true );
    echo date( 'F d, Y', $end_date );
}

if ( 'event_venue' == $column_name ) {
    $venue = get_post_meta( $post_id, 'event-venue', true );
    echo $venue;
}
}
add_action( 'manage_event_posts_custom_column', 'uep_custom_columns_content', 10, 2 );

function create_eventcategory_taxonomy() {

$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' ),
);

register_taxonomy('tf_eventcategory','show', array(
'label' => __('Show Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'show-category' ),
));


$labels = array(
'name' => _x( 'Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Tags' ),
'popular_items' => __( 'Popular Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag' ),
'separate_items_with_commas' => __( 'Separate tag with commas' ),
'add_or_remove_items' => __( 'Add or remove tags' ),
'choose_from_most_used' => __( 'Choose from the most used tags' ),
);

register_taxonomy('tf_eventtag','show', array(
'label' => __('Show Tag'),
'labels' => $labels,
'hierarchical' => false,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'tag' ),
));


$labels = array(
'name' => _x( 'Venue', 'taxonomy general name' ),
'singular_name' => _x( 'Venues', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Venues' ),
'popular_items' => __( 'Popular Venues' ),
'all_items' => __( 'All Venues' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Venue' ),
'update_item' => __( 'Update Venue' ),
'add_new_item' => __( 'Add New Venue' ),
'new_item_name' => __( 'New Venue' ),
'separate_items_with_commas' => __( 'Separate venues with commas' ),
'add_or_remove_items' => __( 'Add or remove venues' ),
'choose_from_most_used' => __( 'Choose from the most used venues' ),
);

register_taxonomy('tf_evenvenue','show', array(
'label' => __('Show Venue'),
'labels' => $labels,
'hierarchical' => false,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'venue' ),
));

}

add_action( 'init', 'create_eventcategory_taxonomy', 0 );
  • What have you coded so far? Please paste your code. – Brad Dalton Feb 3 '15 at 14:48
  • Are you trying to have more than one of the SAME metabox? I'm not sure I quite understand the question. – mor7ifer Feb 3 '15 at 15:16
  • Here's 2 ways to do it stackoverflow.com/a/19230811/2589071 – Brad Dalton Feb 3 '15 at 15:16
  • @m0r7if3r yes i am trying to add same type of metabox so say show1 can have first metabox like 1/1/2015 venue1 gogole.com and 2nd metabox 1/2/2015 venue2 yahoo.com – bob_1982 Feb 3 '15 at 17:25
  • 1
    Why do you need multiple instance of a metabox? Can't you have a single metabox that can add multiple events? That's what in some metabox framework is called a "repeater field". – gmazzap Feb 3 '15 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.