您可以使用posts_clauses
过滤器:
function wpse155797_posts_clauses( $pieces, $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return $pieces;
}
global $wpdb;
if ( ( $orderby = $query->get( \'orderby\' ) ) == \'asset_type\' ) {
if ( ( $order = strtoupper( $query->get( \'order\' ) ) ) != \'DESC\' ) $order = \'ASC\';
$pieces[ \'join\' ] .= \' LEFT JOIN \' . $wpdb->term_relationships . \' AS tr ON \' . $wpdb->posts . \'.ID = tr.object_id\'
. \' LEFT JOIN \' . $wpdb->term_taxonomy . \' AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id\'
. \' LEFT JOIN \' . $wpdb->terms . \' AS t ON tt.term_id = t.term_id\';
$pieces[ \'fields\' ] .= \', group_concat(t.name ORDER BY t.name \' . $order . \') AS \' . $orderby;
$pieces[ \'groupby\' ] = $wpdb->posts . \'.ID\';
$pieces[ \'orderby\' ] = $orderby . \' \' . $order . \', \' . $wpdb->posts . \'.post_title ASC\';
}
return $pieces;
}
add_filter( \'posts_clauses\', \'wpse155797_posts_clauses\', 10, 2 );
这假设您已添加
$columns[\'taxonomy-asset_type\'] = \'asset_type\';
到您的
manage_edit_sortable_columns
滤器
这是按分类术语排序的,但如果按降序排序,并且每个组仍按字母顺序列出多个术语,则看起来很有趣。要解决此问题,需要覆盖默认值\'taxonomy-asset_type\'
中的列manage_posts_columns
过滤器为eg\'asset_type\'
(更改manage_edit_sortable_columns
,然后在manage_posts_custom_column
添加了array_reverse
在输出上:
case \'asset_type\':
$taxonomy = \'asset_type\';
if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
$out = array();
foreach ( $terms as $t ) {
$posts_in_term_qv = array();
$posts_in_term_qv[\'taxonomy\'] = $taxonomy;
$posts_in_term_qv[\'term\'] = $t->slug;
$out[] = sprintf( \'<a href="%s">%s</a>\',
esc_url( add_query_arg( $posts_in_term_qv, \'upload.php\' ) ),
esc_html( sanitize_term_field( \'name\', $t->name, $t->term_id, $taxonomy, \'display\' ) )
);
}
// New bit
if ( isset( $_GET[\'order\'] ) && $_GET[\'order\'] == \'desc\' ) {
$out = array_reverse( $out );
}
/* translators: used between list items, there is a space after the comma */
echo join( __( \', \' ), $out );
} else {
echo \'—\';
}
break;