要实现将自定义可排序列添加到WP_List_Table
对于WordPress管理后端仪表板中的帖子类型,您需要执行以下操作:
替换所有出现的YOUR-POST-TYPE-NAME
使用您的实际职位类型名称替换所有出现的YOUR-TAXONOMY-NAME
使用实际的分类名称替换所有出现的YOUR COLUMN NAME
使用实际列名替换所有出现的YOUR-COLUMN-SLUG
使用实际列段塞
步骤1添加其他WordPress管理表列
if(!function_exists(\'mbe_change_table_column_titles\')){
function mbe_change_table_column_titles($columns){
unset($columns[\'date\']);// temporarily remove, to have custom column before date column
$columns[\'YOUR-COLUMN-SLUG\'] = \'YOUR COLUMN NAME\';
$columns[\'date\'] = \'Date\';// readd the date column
return $columns;
}
add_filter(\'manage_YOUR-POST-TYPE-NAME_posts_columns\', \'mbe_change_table_column_titles\');
}
步骤2
将所有分配的可链接分类术语添加为自定义WordPress管理表列中的行数据
if(!function_exists(\'mbe_change_column_rows\')){
function mbe_change_column_rows($column_name, $post_id){
if($column_name == \'YOUR-COLUMN-SLUG\'){
echo get_the_term_list($post_id, \'YOUR-TAXONOMY-NAME\', \'\', \', \', \'\').PHP_EOL;
}
}
add_action(\'manage_YOUR-POST-TYPE-NAME_posts_custom_column\', \'mbe_change_column_rows\', 10, 2);
}
步骤3启用自定义WordPress管理表列可排序
if(!function_exists(\'mbe_change_sortable_columns\')){
function mbe_change_sortable_columns($columns){
$columns[\'YOUR-COLUMN-SLUG\'] = \'YOUR-COLUMN-SLUG\';
return $columns;
}
add_filter(\'manage_edit-YOUR-POST-TYPE-NAME_sortable_columns\', \'mbe_change_sortable_columns\');
}
步骤4修改
post_clauses
允许按分类术语对自定义WordPress管理表列进行排序
if(!function_exists(\'mbe_sort_custom_column\')){
function mbe_sort_custom_column($clauses, $wp_query){
global $wpdb;
if(isset($wp_query->query[\'orderby\']) && $wp_query->query[\'orderby\'] == \'YOUR-COLUMN-SLUG\'){
$clauses[\'join\'] .= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
$clauses[\'where\'] .= "AND (taxonomy = \'YOUR-TAXONOMY-NAME\' OR taxonomy IS NULL)";
$clauses[\'groupby\'] = "object_id";
$clauses[\'orderby\'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC)";
if(strtoupper($wp_query->get(\'order\')) == \'ASC\'){
$clauses[\'orderby\'] .= \'ASC\';
} else{
$clauses[\'orderby\'] .= \'DESC\';
}
}
return $clauses;
}
add_filter(\'posts_clauses\', \'mbe_sort_custom_column\', 10, 2);
}
调整自定义WordPress管理表列的宽度
if(!function_exists(\'mbe_print_admin_styles\')){
function mbe_print_admin_styles(){
if(!is_admin()){
return false;
}
global $pagenow;
if($pagenow == \'edit.php\' && isset($_GET[\'post_type\']) && $_GET[\'post_type\'] == \'YOUR-POST-TYPE-NAME\'){
echo \'
<style type="text/css">
.column-YOUR-COLUMN-SLUG{
width: 10%;
}
</style>
\'.PHP_EOL;
}
}
add_action(\'wp_print_scripts\', \'mbe_print_admin_styles\');
}
感谢
@goto10
谢谢你的邀请
Sortable admin columns, when data isn\'t coming from post_meta 和
@scribu
用于过帐
Custom Sortable Columns 和
Sortable Taxonomy Columns 对于原始问题的回答
Custom Table Column Sortable by Taxonomy Query.