$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How can I add columns to custom post tables|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How can I add columns to custom post tables

matteradmin10PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 10

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!

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far