繁体   English   中英

在 Wordpress 管理员中使用 ACF Pro 选择字段的可排序自定义列用于帖子列表

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

用于在 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 中创建的选择字段中提取标签,并看到这些标签填充在“编辑状态”列中。 (请参阅上面代码的工作部分。)尽管尝试了不同的教程,但我无法弄清楚如何使该列可排序。

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

// 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 选择字段时有效的解决方案,所以我觉得它与pre_get_posts和使用pre_get_posts的选择get_field_object ,但我不确定。

任何反馈表示赞赏,因为我无法弄清楚我哪里出错了! 我知道有一些插件可以为 Wordpress 创建自定义可排序列。 但是,为了学习,我想知道我在这里做错了什么。 谢谢!

无法为您提供代码方面的帮助,但是,如果您不厌其烦地工作,则可以参阅Admin Columns Pro

让我们轻松地为帖子或页面列表(或任何CPT /分类法)创建列,并可以将这些列设置为可在线编辑,可排序,可过滤等。

本可以作为评论,但要点还不够。 抱歉。

看来我们在尝试完成此类任务时遇到了相同的指南。

这就是最终为我工作的原因。

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

“目录”是帖子类型。 “电子邮件”是 ACF 名称

添加列

add_filter('manage_directory_posts_columns', 'filter_directory_custom_columns');

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

用发布数据填充行

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) 社区大喊大叫,感谢他们帮助我解决了这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM