WordPress – make a custom admin column of a custom post type sortable (the column is a Taxonomy)

huangapple go评论95阅读模式
英文:

WordPress - make a custom admin column of a custom post type sortable (the column is a Taxonomy)

问题

我创建了一个名为property的自定义文章类型(CPT),该CPT具有名为property_type的分类法。
在管理部分,我使用以下代码创建了一个自定义列,以显示property_type分类法在CPT列表中:

  1. add_filter('manage_property_posts_columns', function($columns){
  2. return [
  3. 'cb' => $columns['cb'],
  4. 'title' => $columns['title'],
  5. 'type' => '类型',
  6. 'date' => $columns['date']
  7. ];
  8. });

以及这个:

  1. add_filter('manage_property_posts_custom_column', function($column, $postId){
  2. if($column === 'type'){
  3. $typePost = wp_get_post_terms($postId,['property_type']);
  4. echo $typePost[0]->name;
  5. }
  6. }, 10, 2);

这部分代码运行正常。现在我想让这一列可排序。

在我的研究中,我找到了以下内容来使列标题可排序:

  1. function register_sortable_columns( $columns ) {
  2. $columns['type'] = 'type';
  3. return $columns;
  4. }
  5. add_filter( 'manage_edit-property_sortable_columns', 'register_sortable_columns' );

以及以下内容来管理排序:

  1. add_action( 'pre_get_posts', 'my_slice_orderby' );
  2. function my_slice_orderby( $query ) {
  3. if( ! is_admin() )
  4. return;
  5. $orderby = $query->get( 'orderby');
  6. if( 'type' == $orderby ) {
  7. $query->set('meta_key','type');
  8. $query->set('orderby','meta_value');
  9. }
  10. }

但是当我点击排序时,列表为空白。有任何想法吗?
谢谢!

英文:

i created a custom post type called property, this CPT has a taxonomy called property_type.
In the admin section I created a custom column to show the property_type taxonomy in the CTP listing with this code:

  1. add_filter('manage_property_posts_columns', function($columns){
  2. return [
  3. 'cb' => $columns['cb'],
  4. 'title' => $columns['title'],
  5. 'type' => 'Type',
  6. 'date' => $columns['date']
  7. ];
  8. });

and this

  1. add_filter('manage_property_posts_custom_column', function($column, $postId){
  2. if($column ==='type'){
  3. $typePost = wp_get_post_terms($postId,['property_type']);
  4. echo $typePost[0]->name;
  5. }
  6. }, 10, 2);

That works fine. Now I want to make this column sortable.

In my reseach I found this to make the column title sortable

  1. function register_sortable_columns( $columns ) {
  2. $columns['type'] = 'type';
  3. return $columns;
  4. }
  5. add_filter( 'manage_edit-property_sortable_columns', 'register_sortable_columns' );

and this to manage to sort:

  1. add_action( 'pre_get_posts', 'my_slice_orderby' );
  2. function my_slice_orderby( $query ) {
  3. if( ! is_admin() )
  4. return;
  5. $orderby = $query->get( 'orderby');
  6. if( 'type' == $orderby ) {
  7. $query->set('meta_key','type');
  8. $query->set('orderby','meta_value');
  9. }
  10. }

but when i click on the sort, i have a blank listing.
Any idea please?
thanks

答案1

得分: -2

以下是您提供的代码的翻译:

找到了解决方案。对于那些感兴趣的人:

  1. function register_sortable_columns( $columns ) {
  2. $columns['type'] = 'type';
  3. return $columns;
  4. }
  5. add_filter( 'manage_edit- property_sortable_columns','register_sortable_columns' );
  6. // 自定义“类型”列的排序
  7. add_action( 'pre_get_posts', 'my_property_type_orderby' );
  8. function my_property_type_orderby( $query ) {
  9. if ( ! is_admin() )
  10. return;
  11. $orderby = $query->get( 'orderby' );
  12. if ( 'type' == $orderby ) {
  13. $query->set( 'orderby', 'taxonomy__field' );
  14. // 检查当前排序是升序还是降序,并反转它
  15. $order = strtoupper( $query->get( 'order' ) );
  16. if ( $order === 'ASC' ) {
  17. $query->set( 'order', 'DESC' );
  18. } else {
  19. $query->set( 'order', 'ASC' );
  20. }
  21. }
  22. }
  23. // 处理自定义排序
  24. add_filter( 'posts_clauses', 'my_custom_tax_orderby', 10, 2 );
  25. function my_custom_tax_orderby( $clauses, $query ) {
  26. global $wpdb;
  27. if ( isset( $query->query['orderby'] ) && 'taxonomy__field' === $query->query['orderby'] ) {
  28. $clauses['join'] .= "
  29. LEFT JOIN $wpdb->term_relationships AS tr ON ($wpdb->posts.ID = tr.object_id)
  30. LEFT JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
  31. LEFT JOIN $wpdb->terms AS t ON (tt.term_id = t.term_id)
  32. ";
  33. $clauses['where'] .= " AND (taxonomy = 'property_type' AND tt.taxonomy = 'property_type')";
  34. $clauses['groupby'] = "($wpdb->posts.ID)";
  35. $clauses['orderby'] = "t.name " . $query->get( 'order' ); // 使用查询中的顺序
  36. }
  37. return $clauses;
  38. }

希望这有助于您理解代码。如果您有任何问题,请随时提问。

英文:

Found the solution. For those interested:

  1. function register_sortable_columns( $columns ) {
  2. $columns['type'] = 'type';
  3. return $columns;
  4. }
  5. add_filter( 'manage_edit- property_sortable_columns','register_sortable_columns' );
  6. // Custom sorting for the "type" column
  7. add_action( 'pre_get_posts', 'my_property_type_orderby' );
  8. function my_property_type_orderby( $query ) {
  9. if ( ! is_admin() )
  10. return;
  11. $orderby = $query->get( 'orderby' );
  12. if ( 'type' == $orderby ) {
  13. $query->set( 'orderby', 'taxonomy__field' );
  14. // Check if the current order is ascending or descending and reverse it
  15. $order = strtoupper( $query->get( 'order' ) );
  16. if ( $order === 'ASC' ) {
  17. $query->set( 'order', 'DESC' );
  18. } else {
  19. $query->set( 'order', 'ASC' );
  20. }
  21. }
  22. }
  23. // Handle custom sorting
  24. add_filter( 'posts_clauses', 'my_custom_tax_orderby', 10, 2 );
  25. function my_custom_tax_orderby( $clauses, $query ) {
  26. global $wpdb;
  27. if ( isset( $query->query['orderby'] ) && 'taxonomy__field' === $query->query['orderby'] ) {
  28. $clauses['join'] .= "
  29. LEFT JOIN $wpdb->term_relationships AS tr ON ($wpdb->posts.ID = tr.object_id)
  30. LEFT JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
  31. LEFT JOIN $wpdb->terms AS t ON (tt.term_id = t.term_id)
  32. ";
  33. $clauses['where'] .= " AND (taxonomy = 'property_type' AND tt.taxonomy = 'property_type')";
  34. $clauses['groupby'] = "($wpdb->posts.ID)";
  35. $clauses['orderby'] = "t.name " . $query->get( 'order' ); // Use the order from the query
  36. }
  37. return $clauses;

}

huangapple
  • 本文由 发表于 2023年7月23日 15:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定