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

Php 使用Wordpress admin for post列表中的ACF Pro select字段对自定义列进行排序,php,wordpress,advanced-custom-fields,acfpro,Php,Wordpress,Advanced Custom Fields,Acfpro,在Wordpress管理员中为帖子列表创建新列的代码: //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 statu

在Wordpress管理员中为帖子列表创建新列的代码:

//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;

  }
}
问题: 我正在从每篇文章中成功地从我在Advanced Custom Fields Pro中创建的select字段中拉入标签,并看到这些标签填充在“编辑状态”列中。(请参阅上面代码的工作部分。)尽管尝试了不同的教程,但我不知道如何使该列可排序

代码的非工作部分如下所示。这段代码并没有破坏站点-该列仍然是不可排序的

// 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管理员的帖子列表页面上设置“编辑状态”栏。我希望能够按编辑状态按字母顺序排序(例如,草稿、待处理、正在审查等)

以上所有代码目前都在我创建的自定义插件中。我见过在不使用ACF Pro select字段时有效的解决方案,所以我觉得这与
获取前的帖子
和使用select with
获取字段对象
中的元有关,但我不确定


感谢您的反馈,因为我不知道哪里出了问题!我知道有一些插件可以为Wordpress创建自定义的可排序列。然而,为了学习,我想知道我在这里做错了什么。谢谢

无法帮助您编写代码,但如果您已经厌倦了编写代码,您可能会求助于

让您轻松地为帖子或页面列表(或任何CPT/分类法)创建列,并且可以将这些列设置为内联可编辑、可排序、可筛选等


本应将此作为评论,但没有足够的观点。对不起。

是的,我以前用过这个插件,它很棒。这一次我只是想从头开始建造它——好消息,我刚刚让它工作了!我将很快发布解决方案。谢谢如果你找到了一个解决方案(如你提到的),请考虑在这里张贴并把它标记为答案!