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?
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?
Share Improve this question edited Sep 23, 2014 at 9:41 Femi asked Sep 22, 2014 at 16:32 FemiFemi 473 silver badges6 bronze badges 2- By "meta box as a column", you mean adding the meta value into a table cell in the WP List Table? – kaiser Commented Sep 22, 2014 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 Commented Sep 22, 2014 at 16:43
1 Answer
Reset to default 10I 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!