2

I have created a custom post type. I also have added a meta box to my custom post type. What I want to do now is add my meta box as columns to my custom post table.

My Custom Post

add_action( 'init', 'create_post_type' );
// Create my custom post
function create_post_type() {
    register_post_type( 'spark_stars', 
        array('labels' => 
            array(
                'name' => __( 'Stars' ),
           'singular_name' => __( 'Star' )),
           'public' => true,
           'has_archive' => true,
           'supports' => array( 'title', 'editor', 'thumbnail'),
        )
    );
 }

add_action('add_meta_boxes','stars_meta_box');
// Create my meta box
function stars_meta_box(){
    global $post;
    add_meta_box('first_name_meta_box','First Name',
        'first_name_meta_box_html','spark_stars','normal','default');
}
// Create meta box html
function first_name_meta_box_html(){
    wp_nonce_field('first_name','first_name_meta_box_nonce');
    $value = get_post_meta(get_the_ID(), 'first_name_meta_box_key', true ); ?>
    <label>First Name: </label>
        <input type="text" name="fname" 
            value="<?php echo esc_attr($value); ?>"/>
<?php { 

add_action('manage_spark_stars_posts_columns',.....) // is this the function?
add_filter('manage_spark_stars_posts_columns',.....) // is this the function?

How can I get this meta box as a column in my custom post table and also how can I get the thumbnail of each post as a column in my custom post table?

  • By "meta box as a column", you mean adding the meta value into a table cell in the WP List Table? – kaiser Sep 22 '14 at 16:39
  • Yes. The title of the meta box will be the name of the column and its value will be the content. – Femi Sep 22 '14 at 16:43
6

I think the manage_{$post_type}_posts_columns filter is what you're looking for. You can then use the manage_posts_custom_column action to manage the content for each column in the posts list view.

EDIT::

To add custom columns to your custom post type, you need to filter the columns being output using the manage_{$post_type}_posts_columns where $post_type is the name you used to register the custom post type. In your case it would be spark_stars.

The $columns variable is an array of the current columns. You can either add to it completely override it as necessary.

add_filter('manage_spark_stars_posts_columns','filter_cpt_columns');

function filter_cpt_columns( $columns ) {
    // this will add the column to the end of the array
    $columns['first_name'] = 'First Name';
    //add more columns as needed

    // as with all filters, we need to return the passed content/variable
    return $columns;
}

The next step is to tell WordPress what content needs to be displayed in the column. This can be done with the manage_posts_custom_column action. The method below outputs the First Name if the post meta exists or a default string if not.

add_action( 'manage_posts_custom_column','action_custom_columns_content', 10, 2 );
function action_custom_columns_content ( $column_id, $post_id ) {
    //run a switch statement for all of the custom columns created
    switch( $column_id ) { 
        case 'first_name':
            echo ($value = get_post_meta($post_id, 'first_name_meta_box_key', true ) ) ? $value : 'No First Name Given';
        break;

        //add more items here as needed, just make sure to use the column_id in the filter for each new item.

   }
}

Hopefully this is more clear!

  • 1
    Answers should be more than plain links. They should actually be an answer instead of a route where someone will maybe find an answer. Please help preventing link rot, edit your answer and provide the needed information that helps the OP as well as later visitors to solve their problem. Maybe add an example on how to use the filter - in context of the question. – kaiser Sep 22 '14 at 17:52
  • I can't believe WordPress is coded this poorly. Adding a column to the list of a custom post type in the admin should be a matter of registering that column in the custom post type's admin object, not calling some random global functions. – kloddant Jan 17 at 18:35
  • @kloddant I'm not sure what you're referring to. There is no random global function. Hooks are the paradigm that WordPress uses to allow customization. Are you referring to add_action and add_filter? – Welcher Mar 27 at 13:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.