钩子文档

manage_posts_columns

💡 云策文档标注

概述

manage_posts_columns 是一个 WordPress 过滤器,用于自定义文章列表表格中显示的列。它适用于所有文章类型(除页面外),开发者可以通过此 Hook 添加、移除或修改列标题。

关键要点

  • 过滤器参数:$posts_columns(关联数组,列标题)和 $post_type(文章类型 slug)。
  • 适用范围:所有文章类型,不包括页面(页面使用 manage_pages_columns)。
  • 自定义列:通过添加数组项到 $posts_columns,键为列 ID,值为显示文本。
  • 内置列类型:包括 cb(复选框)、title(标题)、author(作者)、categories(分类)、tags(标签)、comments(评论数)、date(日期)等。
  • 相关 Hook:使用 manage_posts_custom_column 来渲染自定义列的内容。

代码示例

// 添加自定义列示例
function my_add_new_columns($columns) {
    $post_type = get_post_type();
    if ( $post_type == 'post' ) {
        $new_columns = array(
            'my_featured' => esc_html__( 'Featured', 'text_domain' ),
        );
        return array_merge($columns, $new_columns);
    }
    return $columns; // 必须返回默认值以避免影响其他文章类型
}
add_filter( 'manage_posts_columns', 'my_add_new_columns' );

注意事项

  • 添加自定义列时,需确保函数返回默认 $columns 值,以防破坏其他文章类型的列显示。
  • 对于特定自定义文章类型,建议使用 manage_{$post_type}_posts_columns 过滤器。
  • 自定义列的内容显示需配合 manage_posts_custom_column 动作钩子实现。

📄 原文内容

Filters the columns displayed in the Posts list table.

Parameters

$posts_columnsstring[]
An associative array of column headings.
$post_typestring
The post type slug.

More Information

  • manage_posts_columns is a filter applied to the columns shown on the manage posts screen. It’s applied to posts of all types except pages. To add a custom column for pages, hook the manage_pages_columns filter. To add a custom column for specific custom post types, hook the manage_{$post_type}_posts_columns filter.
  • Built-in Column Types
    Listed in order of appearance. By default, all columns supported by the post type are shown.

    • cb Checkbox for bulk actions.
    • title Post title. Includes “edit”, “quick edit”, “trash” and “view” links. If $mode (set from $_REQUEST[‘mode’]) is ‘excerpt’, a post excerpt is included between the title and links.
    • author Post author.
    • categories Categories the post belongs to.
    • tags Tags for the post.
    • comments Number of pending comments.
    • date The date and publish status of the post.

Source

$posts_columns = apply_filters( 'manage_posts_columns', $posts_columns, $post_type );

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 10 content

    To add a custom column, hook into this filter, and add an item to the $post_columns array. The key should be a string ID, and the value should be the human-readable text to display in the column’s header.

    ID, '_demo_publisher', true ) );
    }
    add_action( 'manage_posts_custom_column',  __NAMESPACE__ . '\render_column' );

  2. Skip to note 11 content

    If the new custom column is only for the default post type, then it needs to check the post type.

    function my_add_new_columns($columns) {
        $post_type = get_post_type();
        if ( $post_type == 'post' ) {
            $new_columns = array(
                'my_featured' => esc_html__( 'Featured', 'text_domain' ),
            );
            return array_merge($columns, $new_columns);
        }
    }
    add_filter( 'manage_posts_columns',  'my_add_new_columns' );

  3. Skip to note 12 content

    Example: To add custom featured image thumbnail column in the post.

    function wpdocs_posts_thumb_columns( $columns ) {
        $post_new_columns = array(
           'post_thumbs' => esc_html__( 'Thumbs', 'text_domain' ),
        );
        return array_merge( $columns, $post_new_columns );
    }
    add_filter( 'manage_posts_columns', 'wpdocs_posts_thumb_columns', 5 );
    
    function wpdocs_posts_custom_columns( $column_name, $id ) {
        if ( 'post_thumbs' === $column_name ) {
            the_post_thumbnail( 'thumbnail' );
        }
    }
    add_action( 'manage_posts_custom_column', 'wpdocs_posts_custom_columns', 5, 2 );

  4. Skip to note 13 content

    Example migrated from Codex:

    To add a column showing whether a post is sticky or not:

    function add_sticky_column( $columns ) {
    	$columns['sticky'] = __('Sticky');
            return $columns;
    }
    add_filter( 'manage_posts_columns' , 'add_sticky_column' );

    To actually display whether or not a post is sticky, hook the manage_posts_custom_column action.

  5. Skip to note 14 content

    To add and remove columns from backand posts > all post .

    if ( ! function_exists( 'wpdocs_manage_custom_posts_column' ) ) {
        function wpdocs_manage_custom_posts_column( $columns ) {
    	// delete some columns from dashboard post page . 
    
            unset( $columns['categories'] );
            unset( $columns['tags'] );
    		
    	// add a customs column in dashboard post page .  
    	$post_type = get_post_type();
    	if ( $post_type == 'post' ) {
    		$new_columns = array(
    			'my_featured' => esc_html__( 'Featured', 'aquila' ),
    			'my_column' => esc_html__( 'My Column ', 'aquila' ),
    		);
    		$custom_columns = array_merge($columns, $new_columns);
    	}
    
    	// return final custom columns variable . 
            return $custom_columns;
        }
        add_filter( 'manage_posts_columns', 'wpdocs_manage_custom_posts_column' );
    }

  6. Skip to note 15 content

    To add and remove columns from backand posts > all post .

    if ( ! function_exists( 'custom_manage_posts_column' ) ) {
        function manage_custom_posts_column( $columns ) {
    		// delete some columns from dashboard post page . 
            unset( $columns['categories'] );
            unset( $columns['tags'] );		
    		// add a customs column in dashboard post page .  
    		$post_type = get_post_type();
    		if ( $post_type == 'post' ) {
    			$new_columns = array(
    				'my_featured' => esc_html__( 'Featured', 'text_domain' ),
    				'my_column'   => esc_html__( 'My Column', 'text_domain' ),
    			);
    			$custom_columns = array_merge($columns, $new_columns);
    		}
    	// return final custom columns variable . 
            return $custom_columns;
        }
    }
        add_filter( 'manage_posts_columns', 'manage_custom_posts_column' );

  7. Skip to note 16 content

    Example: This will remove the author, categories, tags and comment columns from backend Posts > All Posts section.

    if ( ! function_exists( 'custom_manage_posts_column' ) ) {
    	function custom_manage_posts_column( $columns ) {
    		unset( $columns['author'] );
    		unset( $columns['categories'] );
    		unset( $columns['tags'] );
    		unset( $columns['comments'] );
    		return $columns;
    	}
    }
    if ( has_filter( 'manage_posts_columns' ) ) {
    	add_filter( 'manage_posts_columns', 'custom_manage_posts_column' );
    }