我有一个名为Member的自定义post类型,我已将代码添加到我的函数中。php文件为名字、姓氏和成员类型添加两个自定义列标题(这很有效),但我无法从a-Z排序默认的post\\u title列。我确信这与title vs post\\u title或其他内容有关,但这对我来说是新的。我做错了什么?
/*add custom columns to the member post type*/
add_filter( \'manage_edit-member_columns\', \'my_edit_member_columns\' );
function my_edit_member_columns( $columns ) {
$columns = array(
\'cb\' => \'<input type="checkbox" />\',
\'title\' => __( \'Member\' ),
\'first_name\' => __( \'First\' ),
\'last_name\' => __( \'Last\' ),
\'member_type\' => __( \'Member Type\' ),
\'date\' => __( \'Date\' )
);
return $columns;
}
/*add hooks to add content to the columns*/
add_action( \'manage_member_posts_custom_column\', \'my_manage_member_columns\', 10, 2 );
function my_manage_member_columns( $column, $post_id ) {
global $post;
switch( $column ) {
/* If displaying the \'first name\' column. */
case \'first_name\' :
/* Get the post meta. */
$first_name = get_post_meta( $post_id, \'first_name\', true );
/* If no first_name is found, output a default message. */
if ( empty( $first_name ) )
echo ( \'Unknown\' );
else
printf( $first_name );
break;
/* If displaying the \'last_name\' column. */
case \'last_name\' :
/* Get the post meta. */
$last_name = get_post_meta( $post_id, \'last_name\', true );
/* If no first_name is found, output a default message. */
if ( empty( $last_name ) )
echo ( \'Unknown\' );
else
printf( $last_name );
break;
/* If displaying the \'member_type\' column. */
case \'member_type\' :
/* Get the post meta. */
$member_type = get_post_meta( $post_id, \'member_type\', true );
/* If no first_name is found, output a default message. */
if ( empty( $member_type ) )
echo ( \'Unknown\' );
else
printf( $member_type );
break;
/* Just break out of the switch statement for everything else. */
default :
break;
}
}
//make the title sortable
add_filter( \'manage_edit-member_sortable_columns\', \'my_member_sortable_columns\' );
function my_member_sortable_columns( $columns ) {
$columns[\'post_title\'] = \'post_title\';
return $columns;
}
// now do the sorting of the member post type
// Only run our customization on the \'edit.php\' page in the admin.
add_action( \'load-edit.php\', \'my_edit_member_load\' );
function my_edit_member_load() {
add_filter( \'request\', \'my_sort_member\' );
}
/* Sorts the member. */
function my_sort_member( $vars ) {
/* Check if we\'re viewing the \'member\' post type. */
if ( isset( $vars[\'post_type\'] ) && \'member\' == $vars[\'post_type\'] ) {
/* Check if \'orderby\' is set to \'title\'. */
if ( isset( $vars[\'orderby\'] ) && \'post_title\' == $vars[\'orderby\'] ) {
/* Merge the query vars with our custom variables. */
$vars = array_merge(
$vars,
array(
\'meta_key\' => \'post_title\',
\'orderby\' => \'meta_value\', /*use meta_value_num if the meta_key is numeric*/
\'order\' => \'asc\'
)
);
}
}
return $vars;
}