使分类列在管理中可排序?

时间:2014-07-25 作者:Steve

我有一个分类术语,已设置为在管理UI中显示为自定义帖子类型的列:

add_action( \'init\', \'create_asset_tax\' );

function create_asset_tax() {
  register_taxonomy(
    \'asset_type\',
    \'design_asset\',
    array(
        \'label\' => __( \'Asset Type\' ),
        \'rewrite\' => array( \'slug\' => \'type\' ),
        \'show_admin_column\' => true,
        \'hierarchical\' => true,
        \'sort\' => true
    )
  );
}
如何在管理UI中将此列设置为自定义帖子类型的“可排序”?我在这里找到的大多数提示似乎都是用于对添加的列进行排序,而不是用于分类术语产生的列,因此有点混乱。谢谢

1 个回复
SO网友:bonger

您可以使用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 \'&#8212;\';
        }
        break;

结束

相关推荐

GET Taxonomy ID

我有一行,其中“5”是分类ID。<?php echo function xyz (5,\'product_cat\'); ?>如何更改此项以使其始终自动识别当前页面的分类ID?尝试此操作但未成功:<?php echo function xyz (get_term_by(\'id\',\'\',\'product_cat);,\'product_cat\'); ?>我该怎么做?谢谢