How do you add custom columns for these custom post types?

I’ve recently been developing a plugin that involves custom post types and taxonomies. What I’m struggling with at the moment is getting the custom post fields to display in the ‘All Reservations’ screen. I can get the columns to display, but cannot for the life of me get the fields within the columns to populate with the data from the database.

My custom post code is below:

Read More
    function reservations_init() {
      $labels = array(
        'name'               => _x( 'Reservations', 'post type general name' ),
        'singular_name'      => _x( 'Reservation', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'reservation' ),
        'add_new_item'       => __( 'Add New Reservation' ),
        'edit_item'          => __( 'Edit Reservation' ),
        'new_item'           => __( 'New Reservation' ),
        'all_items'          => __( 'Reservations' ),
        'view_item'          => __( 'View Reservation' ),
        'search_items'       => __( 'Search Reservations' ),
        'not_found'          => __( 'No reservations found' ),
        'not_found_in_trash' => __( 'No reservations found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Reservations'
      );
      $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our reservations',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'none' ),
        'has_archive'   => true,
        'show_in_nav_menu' => true,
        'show_in_menu' => 'edit.php?post_type=meals',
      );
      register_post_type( 'reservations', $args );
    }
    add_action( 'init', 'reservations_init' );

      function restauranter_add_meta_box() {

          $screens = array( 'reservations' );

          foreach ( $screens as $screen ) {

              add_meta_box(
                  'restauranter_firstname',
                  __( 'Reservation Details', 'restauranter' ),
                  'restauranter_reservation_details',
                  $screen, 
                  'normal', 
                  'high'
              );
          }
      }
      add_action( 'add_meta_boxes', 'restauranter_add_meta_box' );

      function restauranter_reservation_details() {
          global $post;

          echo '<input type="hidden" name="reservationmeta_noncename" id="reservationmeta_noncename" value="' . 
          wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

          $firstname = get_post_meta($post->ID, '_firstname', true);
          $lastname = get_post_meta($post->ID, '_lastname', true);
          $phone = get_post_meta($post->ID, '_phone', true);
          $email = get_post_meta($post->ID, '_email', true);
          $guests = get_post_meta($post->ID, '_guests', true);
          $date = get_post_meta($post->ID, '_date', true);
          $time = get_post_meta($post->ID, '_time', true);

        echo '<p>First Name:</p>';
        echo '<input type="text" name="_firstname" value="' . $firstname  . '" class="widefat" />';
        echo '<p>Last Name:</p>';
        echo '<input type="text" name="_lastname" value="' . $lastname  . '" class="widefat" />';
        echo '<p>Phone Number:</p>';
        echo '<input type="text" name="_phone" value="' . $phone  . '" class="widefat" />';
        echo '<p>Email:</p>';
        echo '<input type="text" name="_email" value="' . $email  . '" class="widefat" />';
        echo '<p>Number of Guests:</p>';
        echo '<input type="text" name="_guests" value="' . $guests  . '" class="widefat" />';
        echo '<p>Reservation Date:</p>';
        echo '<input type="text" name="_date" value="' . $date  . '" class="widefat date" />';
        echo '<p>Reservation Time:</p>';
        echo '<input type="text" name="_time" value="' . $time  . '" class="widefat time" />';
        echo '<input type="submit" name="_time" value="" class="widefat submit" />';

      }
      function restauranter_save_reservations_meta($post_id, $post) {

          if ( !wp_verify_nonce( $_POST['reservationmeta_noncename'], plugin_basename(__FILE__) )) {
          return $post->ID;
          }

          // Is the user allowed to edit the post or page?
          if ( !current_user_can( 'edit_post', $post->ID ))
              return $post->ID;

          $reservations_meta['_firstname'] = $_POST['_firstname'];
          $reservations_meta['_lastname'] = $_POST['_lastname'];
          $reservations_meta['_phone'] = $_POST['_phone'];
          $reservations_meta['_email'] = $_POST['_email'];
          $reservations_meta['_guests'] = $_POST['_guests'];
          $reservations_meta['_date'] = $_POST['_date'];
          $reservations_meta['_time'] = $_POST['_time'];


          foreach ($reservations_meta as $key => $value) {
              if( $post->post_type == 'revision' ) return;
              $value = implode(',', (array)$value);
              if(get_post_meta($post->ID, $key, FALSE)) {
                  update_post_meta($post->ID, $key, $value);
              } else {
                  add_post_meta($post->ID, $key, $value);
              }
              if(!$value) delete_post_meta($post->ID, $key);
          }

      }

      add_action('save_post', 'restauranter_save_reservations_meta', 1, 2); // save the custom fields


    function add_new_reservations_columns($reservations_columns) {
        $new_columns['cb'] = '<input type="checkbox" />';
        $new_columns['_firstname'] = __('First Name', '_firstname');
        return $new_columns;
    }
    add_filter('manage_edit-reservations_columns' , 'add_new_reservations_columns');

I have tried googling, I have tried numerous samples of code, I have tried playing about with the code my self also but still no joy.

Related posts

1 comment

  1. You need one more function to add data into those columns….

    function custom_edit-reservations_columns( $column, $post_id ) {
      switch ( $column ) {
        case "_firstname":
          $firstname = get_post_meta($post_id, '_firstname', true);
          echo $firstname;
        break;
      }
    }
    add_action( "manage_posts_custom_column", "custom_edit-reservations_columns", 10, 2 );
    

    Hope that helps

Comments are closed.