使用 ACF Pro select Wordpress 管理中的字段对 post 列表进行排序的自定义列

Sortable custom column using ACF Pro select field in Wordpress admin for post list

用于在 Wordpress 管理中为 post 列表创建新列的代码:

//adds new column to posts list in Wordpress admin
add_filter( 'manage_posts_columns', 'set_custom_edit_mycpt_columns' );

function set_custom_edit_mycpt_columns( $columns ) {
  $columns['acf_field'] = __( 'Editorial status', 'my-text-domain' );

  return $columns;
}

// pulls label from ACF Pro select field into new column for each post
add_action( 'manage_posts_custom_column' , 'custom_mycpt_column', 10, 2 );
function custom_mycpt_column( $column, $post_id ) {
  switch ( $column ) {

    // display the value of an ACF (Advanced Custom Fields) field
    case 'acf_field' :
      $ed_status = get_field_object( 'ed_status_acf', $post_id ); 
      $ed_status_pretty = $ed_status['label'];
      echo $ed_status_pretty;
      break;

  }
}

问题: 我成功地从每个 post 的 Advanced Custom Fields Pro 中创建的 select 字段中提取标签,并看到这些标签填充在 'Editorial status' 列中。 (请参阅上面代码的工作部分。)尽管尝试了不同的教程,但我无法弄清楚如何使该列可排序。

代码的非工作部分如下所示。此代码不会破坏站点 — 该列只是保持无法排序。

// make new column sortable by ACF field
add_filter( 'manage_edit-posts_sortable_columns', 'set_custom_mycpt_sortable_columns' );

function set_custom_mycpt_sortable_columns( $columns ) {
  $columns['custom_taxonomy'] = 'custom_taxonomy';
  $columns['acf_field'] = 'acf_field';

  return $columns;
}

// give parameters to Wordpress for sorting the new column
add_action( 'pre_get_posts', 'mycpt_custom_orderby' );

function mycpt_custom_orderby( $query ) {
  if ( is_admin() ) {
    return;

  $orderby = $query->get( 'orderby');

  if ( 'acf_field' == $orderby ) {
    $query->set( 'meta_key', 'acf_field' );
    $query->set( 'orderby', 'meta_value' );
  }
  }
}

目标: 弄清楚我做错了什么,并使显示在 Wordpress 管理中的 post 列表页面上的 'Editorial status' 列可排序。我希望能够按编辑状态(例如,草稿、待定、正在审阅等)的字母顺序排序

以上所有代码目前都在我创建的自定义插件中。我已经看到了在不使用 ACF Pro select 字段时有效的解决方案,所以我觉得它与 pre_get_posts 和使用 select 中的元与 get_field_object,但我不确定。

感谢任何反馈,因为我不知道我哪里出错了!我知道有插件可以为 Wordpress 创建自定义可排序列。但是,为了学习,我想知道我在这里做错了什么。谢谢!

无法帮助您编写代码,但如果您厌倦了编写代码,您可能会查看 Admin Columns Pro

让您轻松地为 post 或页面列表(或任何 CPT/taxonomy)创建列,并且您可以将这些列设置为可内联编辑、可排序、可过滤等。

本想把这个作为评论,但是积分不够。对不起。

看来我们 运行 在尝试完成此类任务时跨越了相同的指南。

这就是最终对我有用的东西。

就我而言,我想在管理仪表板中添加一个 ACF 字段作为列,然后使该列可排序。

“目录”是 post 类型。 “email”是 ACF 名称

添加列

add_filter('manage_directory_posts_columns', 'filter_directory_custom_columns');

function filter_directory_custom_columns($columns) {
    $columns['email'] = 'Email';
    return $columns;
}

用 post 数据填充行

add_action('manage_directory_posts_custom_column',  'action_directory_custom_columns');

function action_directory_custom_columns($column) {
    global $post;
    if($column == 'email') {
        $directoryfields = get_fields($post->ID);
        echo $directoryfields['email'];
    }
}

使列可排序

add_filter( 'manage_edit-directory_sortable_columns', 'sortable_directory_custom_columns' );

function sortable_directory_custom_columns( $columns ) {
    $columns['email'] = 'email';
    return $columns;
}

这是我遗漏的部分。

add_action( 'pre_get_posts', 'directory_orderby' );
function directory_orderby( $query ) {
    if( ! is_admin() )
        return;
    $orderby = $query->get( 'orderby');
    if( 'email' == $orderby ) {
        $query->set('meta_key','email');
        $query->set('orderby','meta_value');
    }
}

感谢 UtahWP(jazzsequence, ninnypants) 社区帮助我解决这个问题