在WordPress后端,我想根据元键和元值对自定义帖子类型列进行排序。
元键称为“状态”。对于“状态”键,我有三个不同的元值。如何按字母顺序排序?
这是我的代码:
// Register the column
function status_column_register( $columns ) {
$columns[\'status\'] = __( \'status\', \'bf_rapportage\' );
return $columns;
}
add_filter( \'manage_edit-bf_rapportage_columns\', \'status_column_register\' );
// Display the column content
function status_column_display( $column_name, $post_id ) {
if ( \'status\' != $column_name )
return;
$status = get_post_meta($post_id, \'status\', true);
if ( !$status )
$status = \'<em>\' . __( \'undefined\', \'bf_rapportage\' ) . \'</em>\';
echo $status;
}
add_action( \'manage_posts_custom_column\', \'status_column_display\', 10, 2 );
// Register the column as sortable
function status_column_register_sortable( $columns ) {
$columns[\'status\'] = \'status\';
return $columns;
}
add_filter( \'manage_edit-bf_rapportage_sortable_columns\', \'status_column_register_sortable\' );
function status_column_orderby( $vars ) {
if ( isset( $vars[\'orderby\'] ) && \'status\' == $vars[\'orderby\'] ) {
$vars = array_merge( $vars, array(
\'meta_key\' => \'status\',
\'orderby\' => \'meta_value_num\'
) );
}
return $vars;
}
add_filter( \'request\', \'status_column_orderby\' );