WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/manage posts custom column

Contents

Description

Combined with the manage_${post_type}_posts_columns filter, this allows you to add custom columns to the list post/page/custom post type pages. The action described in here works both for built in post types as well as custom post types. manage_${post_type}_posts_custom_column can be used in WP 3.1 and later for specific custom post types. Note that if the custom post type has 'hierarchical' => true, then the correct action hook to use is manage_pages_custom_column.

Predefined Column Names

The following column filters are already defined and used by WordPress. These can be redefined within a custom the filter switch statement. [See Example]

  • 'cb' - checkbox for selecting post items for bulk actions
  • 'title' - displays the post title as well as post action links (edit, quick edit, trash, view) based on user permissions
  • 'author' - displays the username of the post author as a link to filter post by author
  • 'categories' - displays the post categories as links to filter post by category
  • 'tags' - displays the post tags as links to filter post by tags
  • 'comments' - displays a comment icon with the number of comments as a permalink to manage the comments for that post
  • 'date' - displays the date and status of the post

Parameters

A registered action function is passed the following parameters.

$column_name
(string) (required) The name of the column to display.
Default: None
$post_id
(int) (required) The ID of the current post. Can also be taken from the global $post->ID.
Default: None

Examples

Custom post type

Suppose you have a 'books' custom post type and you want the publish date and book author to show up in the browse page.

add_action( 'manage_posts_custom_column' , 'custom_columns', 10, 2 );

function custom_columns( $column, $post_id ) {
    switch ( $column ) {
	case 'book_author' :
	    $terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
            if ( is_string( $terms ) )
		    echo $terms;
		else
		    _e( 'Unable to get author(s)', 'your_text_domain' );
		break;

	case 'publisher' :
	    echo get_post_meta( $post_id , 'publisher' , true ); 
	    break;
    }
}

Sticky posts

Built-in posts can be made sticky. manage_posts_custom_column can be used to show this in the post list.

/* Display custom column */
function display_posts_stickiness( $column, $post_id ) {
    echo '<input type="checkbox" disabled', ( is_sticky( $post_id ) ? ' checked' : ''), '/>';
}
add_action( 'manage_posts_custom_column' , 'display_posts_stickiness', 10, 2 );

/* Add custom column to post list */
function add_sticky_column( $columns ) {
    return array_merge( $columns, 
        array( 'sticky' => __( 'Sticky', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_sticky_column' );

Source File

As of WP 3.1, manage_posts_custom_column is located in wp-admin/includes/class-wp-posts-list-table.php.

Related

Actions

Filters