钩子文档

manage_{$post->post_type}_posts_custom_column

💡 云策文档标注

概述

manage_{$post->post_type}_posts_custom_column 是一个 WordPress 动作钩子,用于在特定文章类型的文章列表表格中为每个自定义列输出值。它通常与 manage_{$post_type}_posts_columns 过滤器结合使用,以管理自定义文章类型的列显示。

关键要点

  • 这是一个动态钩子,钩子名称中的 $post->post_type 部分指代文章类型,例如 manage_post_posts_custom_column 或 manage_page_posts_custom_column。
  • 钩子接收两个参数:$column_name(字符串,要显示的列名)和 $post_id(整数,当前文章 ID)。
  • 对于内置文章类型和多个自定义类型,可以使用 manage_posts_custom_column 钩子。
  • 在术语和分类法中传递此函数时,会添加第三个参数 $term_id,但 $null 参数未使用。
  • 钩子由 WP_Posts_List_Table::column_default() 方法调用,用于处理默认列输出。
  • 自 WordPress 3.1.0 版本引入。

代码示例

// 示例:为自定义文章类型 'product' 添加自定义列值
function product_custom_column_values( $column, $post_id ) {
    switch ( $column ) {
        case 'product_number':
        case 'product_name':
            echo get_post_meta( $post_id , $column , true );
            break;
        case 'product_buyer':
            $buyer_id = get_post_meta( $post_id , $column , true );
            if( $buyer_id ){
                echo get_post_meta( $buyer_id , 'buyer_name' , true );
            } else {
                echo '';
            }
            break;
    }
}
add_action( 'manage_product_posts_custom_column' , 'product_custom_column_values', 10, 2 );

注意事项

  • 使用此钩子时,需确保与 manage_{$post_type}_posts_columns 过滤器配合,以正确添加或移除列。
  • 对于术语和分类法,注意参数变化,第三个参数 $term_id 是必需的,但 $null 参数无实际用途。
  • 参考相关文档链接,如 manage_post_type_posts_columns 和 list_table_primary_column,以进行更全面的列管理。

📄 原文内容

Fires for each custom column of a specific post type in the Posts list table.

Description

The dynamic portion of the hook name, $post->post_type, refers to the post type.

Possible hook names include:

  • manage_post_posts_custom_column
  • manage_page_posts_custom_column

Parameters

$column_namestring
The name of the column to display.
$post_idint
The current post ID.

More Information

This action is called whenever a value for a custom column should be output for a custom post type. Combined with the manage_${post_type}_posts_columns filter, this allows you to add or remove (unset) custom columns to a list of custom post types.

For built-in post types and multiple custom types, use manage_posts_custom_column.

Terms and Taxonomies

When passing this function on terms and taxonomies, a third parameter is added.

$column_name
(string) (required) The name of the column to display.
Default: None

$term_id
(int) (required) The ID of the current term. Can also be taken from the global $current_screen->taxonomy.
Default: None

$null
(null) (required) Unused and won’t pass anything.
Default: None

Source

do_action( "manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID );

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    // let's say we have a CPT called 'product'
    function product_custom_column_values( $column, $post_id ) {
    
        switch ( $column ) {
    
    		// in this example, a Product has custom fields called 'product_number' and 'product_name'
            case 'product_number'	:
            case 'product_name' 	:
                echo get_post_meta( $post_id , $column , true );
            break;
    
    		// in this example, $buyer_id is post ID of another CPT called "buyer"
            case 'product_buyer' 	:
                $buyer_id = get_post_meta( $post_id , $column , true );
                if( $buyer_id ){
                    echo get_post_meta( $buyer_id , 'buyer_name' , true );
                } else {
                    echo '<div class="dashicons dashicons-minus"></div>';
                }
            break;
    
        }
    }
    add_action( 'manage_product_posts_custom_column' , 'product_custom_column_values', 10, 2 );

    For further management of columns, check:

    https://developer.wordpress.org/reference/hooks/manage_post_type_posts_columns/
    To add/remove/rename columns

    https://developer.wordpress.org/reference/hooks/list_table_primary_column/
    To set the primary (default) column

  2. Skip to note 4 content

    Suppose you have a ‘book’ custom post type and you want to add the publisher and book author in the edit page but remove the post author.

    add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' );
    add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
    
    function set_custom_edit_book_columns($columns) {
        unset( $columns['author'] );
        $columns['book_author'] = __( 'Author', 'your_text_domain' );
        $columns['publisher'] = __( 'Publisher', 'your_text_domain' );
    
        return $columns;
    }
    
    function custom_book_column( $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;
        }
    }