您需要两个附加功能。
第一个将列可排序挂钩到manage_edit-{post_type}_sortable_columns
滤器
function ws_sortable_manufacturer_column( $columns ) {
$columns[\'manufacturer\'] = \'Manufacturer\';
return $columns;
}
add_filter( \'manage_edit-post_sortable_columns\', \'ws_sortable_manufacturer_column\' );
第二个钩子
pre_get_posts
要操作查询以按列排序。。。
function ws_orderby_custom_column( $query ) {
global $pagenow;
if ( ! is_admin() || \'edit.php\' != $pagenow || ! $query->is_main_query() || \'post\' != $query->get( \'post_type\' ) ) {
return;
}
$orderby = $query->get( \'orderby\' );
switch ( $orderby ) {
case \'manufacturer\':
$query->set( \'meta_key\', \'wccaf_manufacturer\' );
$query->set( \'orderby\', \'meta_value\' );
break;
default:
break;
}
}
add_action( \'pre_get_posts\', \'ws_orderby_custom_column\' );
希望有帮助