简体   繁体   English

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

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

Code that works to create new column 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;

  }
}

The problem: I'm successfully pulling in labels from the select field that I created in Advanced Custom Fields Pro from each post and seeing those labels populate in the 'Editorial status' column.问题:我成功地从每个帖子的 Advanced Custom Fields Pro 中创建的选择字段中提取标签,并看到这些标签填充在“编辑状态”列中。 (See working portion of code above.) What I'm not able to figure out is how to make that column sortable, despite trying different tutorials. (请参阅上面代码的工作部分。)尽管尝试了不同的教程,但我无法弄清楚如何使该列可排序。

The non-working portion of the code appears below.代码的非工作部分如下所示。 This code doesn't break the site — the column simply remains unsortable.此代码不会破坏站点——该列仍然无法排序。

// 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' );
  }
  }
}

The goal: Figure out what I'm doing wrong and make the 'Editorial status' column that appears on the post list page in Wordpress admin sortable.目标:找出我做错了什么,并使出现在 Wordpress 管理员的帖子列表页面上的“编辑状态”列可排序。 I'd like to be able to sort alphabetically by editorial status (eg, draft, pending, under review, etc.)我希望能够按编辑状态(例如草稿、待定、审查中等)按字母顺序排序

All code above is currently in a custom plugin that I created.上面的所有代码目前都在我创建的自定义插件中。 I've seen solutions that work when ACF Pro select fields aren't used, so I have a feeling it has to do with pre_get_posts and using the meta from the select with get_field_object , but I'm not sure.我已经看到在不使用 ACF Pro 选择字段时有效的解决方案,所以我觉得它与pre_get_posts和使用pre_get_posts的选择get_field_object ,但我不确定。

Any feedback appreciated, since I can't figure out where I'm going wrong!任何反馈表示赞赏,因为我无法弄清楚我哪里出错了! I know there are plugins to create custom sortable columns for Wordpress.我知道有一些插件可以为 Wordpress 创建自定义可排序列。 I'd like to know what I'm doing wrong here, however, in order to learn.但是,为了学习,我想知道我在这里做错了什么。 Thanks!谢谢!

Can't help you with your code, but if you get to the point where you're tired of working at it you might look in to Admin Columns Pro . 无法为您提供代码方面的帮助,但是,如果您不厌其烦地工作,则可以参阅Admin Columns Pro

Let's you easily create columns for the post or page lists (or any CPT/taxonomy) and you can set those columns to be inline-editable, sortable, filterable, etc. 让我们轻松地为帖子或页面列表(或任何CPT /分类法)创建列,并可以将这些列设置为可在线编辑,可排序,可过滤等。

Would have put this as a comment, but not enough points. 本可以作为评论,但要点还不够。 Sorry. 抱歉。

It appears we ran across the same guides while trying to accomplish this sort of task.看来我们在尝试完成此类任务时遇到了相同的指南。

This is what ended up working for me.这就是最终为我工作的原因。

In my case, I wanted to add an ACF field as a column in the admin dashboard and then make that column sortable.就我而言,我想在管理仪表板中添加一个 ACF 字段作为列,然后使该列可排序。

"directory" is the post type. “目录”是帖子类型。 "email" is the ACF name “电子邮件”是 ACF 名称

add the column添加列

add_filter('manage_directory_posts_columns', 'filter_directory_custom_columns');

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

fill the rows with the post data用发布数据填充行

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'];
    }
}

make the column sortable使列可排序

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

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

This was the part I was missing.这是我缺少的部分。

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');
    }
}

shoutout to the UtahWP(jazzsequence, ninnypants) community for helping me work through this向 UtahWP(jazzsequence, ninnypants) 社区大喊大叫,感谢他们帮助我解决了这个问题

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

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