[РЕШЕНО] Новая колонка для CATEGORY с сортировкой по произвольному полю (алфавитная а не номерная)
как поправить запрос в коде ниже, для правильно сортировке по нумерации, а не по первому символу (алфавитной)?
https://wp-kama.ru/id_995/dopolnitelnyie-sortiruemyie-kolonki-u-postov-v-adminke.html
Создал счетчик просмотров для терминов и Category, значения пишу в views_cat.
Почитал тему, немного поправил код для категорий, а не для постов.
Далее сортировка с которой проблема:
GitHub<?php
/**
* This example uses 'main-category' as a custom taxonomy and values
* from Carbon Fields for sorting https://carbonfields.net
*/
// Add custom column title
add_filter( 'manage_edit-main-category_columns', function( $columns ) {
$column_position = 2;
$before = array_slice( $columns, 0, $column_position, true );
$after = array_slice( $columns, $column_position, count( $columns ), true );
$custom_column = [
'is_public' => __( 'Public', 'your-textdomain' )
];
$columns = array_merge( $before, $custom_column, $after );
return $columns;
}, 10, 3 );
// Render custom column content
add_filter( 'manage_main-category_custom_column', function( $content, $column_name, $term_id ) {
if ( 'is_public' === $column_name ) {
$content = carbon_get_term_meta( $term_id, 'crb_is_public' ) ? "\u{2705}" : "\u{1F6D1}"; // ✅ 🛑
}
return $content;
}, 10, 3 );
// Make custom column sortable
add_filter( 'manage_edit-main-category_sortable_columns', function( $sortable ) {
$sortable[ 'is_public' ] = 'is_public';
return $sortable;
}, 10, 3 );
// Add custom sortable content to taxonomy query
add_filter( 'terms_clauses', function( $pieces, $taxonomies, $args ) {
global $pagenow, $wpdb;
$custom_sort_term = 'is_public';
$custom_taxonomy = 'main-category';
$orderby = ( isset( $_GET[ 'orderby' ] ) ) ? trim( sanitize_text_field( $_GET[ 'orderby' ] ) ) : '';
if ( empty( $orderby ) ) { return $pieces; }
$taxonomy = $taxonomies[ 0 ];
if ( ! is_admin() || 'edit-tags.php' !== $pagenow || ! in_array( $taxonomy, [ $custom_taxonomy ] ) ) {
return $pieces;
}
if ( $custom_sort_term === $orderby ) {
$pieces[ 'join' ] .= ' INNER JOIN ' . $wpdb->termmeta . ' AS tm ON t.term_id = tm.term_id ';
$pieces[ 'orderby' ] = ' ORDER BY tm.meta_value ';
$pieces[ 'where' ] .= ' AND tm.meta_key = "_crb_is_public"';
}
return $pieces;
}, 10, 3 );
сортировка работает, но не по величине числа, а по начальному значению, называется алфавитной, пример
1123
125
2233333222
34
add_filter( 'terms_clauses', function( $pieces, $taxonomies, $args ) {
global $pagenow, $wpdb;
$custom_sort_term = 'views_cat';
$custom_taxonomy = 'category';
$orderby = ( isset( $_GET[ 'orderby' ] ) ) ? trim( sanitize_text_field( $_GET[ 'orderby' ] ) ) : '';
if ( empty( $orderby ) ) { return $pieces; }
$taxonomy = $taxonomies[ 0 ];
if ( ! is_admin() || 'edit-tags.php' !== $pagenow || ! in_array( $taxonomy, [ $custom_taxonomy ] ) ) {
return $pieces;
}
if ( $custom_sort_term === $orderby ) {
$pieces[ 'join' ] .= ' INNER JOIN ' . $wpdb->termmeta . ' AS tm ON t.term_id = tm.term_id ';
$pieces[ 'orderby' ] = ' ORDER BY tm.meta_value ';
$pieces[ 'where' ] .= ' AND tm.meta_key = "views_cat"';
}
return $pieces;
}, 10, 3 );
Решение
tm.meta_value+0