将 ACF 管理列添加到自定义 post 类型

Add ACF admin column to custom post type

我正在尝试使用自定义字段 (ACF) 的内容向我的自定义 post 类型添加一个新的管理列。我要添加的字段是一个 'post object' 字段,但它只显示 post 标题而不是 ACF 中的链接 post。我添加了截图。

这是我目前拥有的:

function add_new_realisaties_column($columns) {
    $columns['realisatie_line'] = 'Line';
    return $columns;
}
add_filter('manage_realisaties_posts_columns', 'add_new_realisaties_column');

function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
if ($column == 'realisatie_line') {
    $post_object = get_field('realisatie_line');

    if( $post_object ):
        // override $post
        $post = $post_object;
        setup_postdata( $post ); 

        $evdate = the_title();

        wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; 

    echo $evdate;
    }
}
add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);

/* Make the column sortable */
function set_custom_realisaties_sortable_columns( $columns ) {
    $columns['realisatie_line'] = 'realisatie_line';
    return $columns;
}
add_filter( 'manage_edit-realisaties_sortable_columns', 'set_custom_realisaties_sortable_columns' );

function realisaties_custom_orderby( $query ) {
    if ( ! is_admin() )
        return;
        $orderby = $query->get('orderby');

        if ( 'realisatie_line' == $orderby ) {
            $query->set( 'meta_key', 'realisatie_line' );
            $query->set( 'orderby', 'meta_value' );
        }
    }
add_action( 'pre_get_posts', 'realisaties_custom_orderby' );

尝试将您的 add_new_realisaties_admin_column_show_value 函数更改为以下代码。如果 ACF 字段名称是 realisatie_line,您还需要传递 $post_id 以获取每个特定 post.

的特定元数据
function add_new_realisaties_admin_column_show_value( $column, $post_id ) {

    //Try using a switch/case for the column name
    switch ( $column ) {
       case 'realisatie_line': //Name of new column from add_new_realisaties_column function
          echo get_the_title( get_post_meta( $post_id, 'realisatie_line', true ) ); //Getting the ACF post meta using the $post_id, passing it through the get_the_title function to get title of attached post
          break;

       default:
          break;

    }
}

我注意到几件事。一个是您实际上不需要使用 setup_postdata() function, because the ACF Post Object field uses get_post() 已经 returns 一个完整的对象。仅当您打算覆盖全局 $post 对象时才这样做,例如在 single-{post_type}.php 模板上。

另一件事是,对于 post 列,使用 switch 语句而不是 if/else 通常更为常见。有点迂腐,但有一点要注意。

最后,the_title() will echo the title by default, so assigning it, and then echoing it later, can cause issues (namely leaving variables littered around the document). Consider using get_the_title() 如果您打算将其分配给变量。此外,我不会深入细节,但仅使用 setup_postdata 可能不足以让 post 辅助函数从您想要的位置提取数据。

现在,综上所述,您应该能够从 get_field(), since it returns a full formed WP_Post object 中回显 $post_objectpost_title 字段。我把它放在我的测试站点上,它按预期工作:

add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
function add_new_realisaties_admin_column_show_value( $column, $post_id ){
    switch( $column ){
        case 'realisatie_line':
            if( $post_object = get_field('realisatie_line') ){
                echo $post_object->post_title;
            }
            break;
    }
}

这是它在管理员中的样子,注意我只是为 Post 对象关系字段随机抓取了一个 post: