我有一些具有不同分类法的自定义帖子类型,我有一个功能,可以在编辑中向帖子列表视图添加某些类别。php管理屏幕。那里一切都很好。我还添加了一个函数,允许用户按这些类别进行排序。这就是问题所在。当你按名称或日期排序时,一切都很好,但当你按自定义类别排序时,所有帖子都会消失,我不知道为什么。
这是通过自定义post类型注册返回的所有代码。所讨论的代码是最后一块,但我认为人们可能需要在上下文中看到整个内容
// Add custom post type Artists
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
register_post_type( \'band_artists\',
array(
\'labels\' => array(
\'name\' => __( \'Artists\' ),
\'singular_name\' => __( \'Artist\' ),
\'add_new\' => __( \'Add New\',\'Artist\' ),
\'add_new_item\' => __( \'Add New Artist\' ),
\'edit_item\' => __( \'Edit Artist\' ),
\'new_item\' => __( \'New Artist\' ),
\'view_item\' => __( \'View Artist\' ),
\'search_items\' => __( \'Search Artists\' ),
\'not_found\' => __( \'No Artists Found\' ),
\'not_found_in_trash\' => __( \'No Artists In Trash\' ),
\'parent_item_colon\' => \'\'
),
\'public\' => true,
\'has_archive\' => true,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'excerpt\', \'custom-fields\', \'thumbnail\' ),
\'rewrite\' => array(\'slug\' => \'artists\'),
\'taxonomies\' => array(\'category\', \'post_tag\')
)
);
register_taxonomy(\'large_feature\', array(\'band_artists\'), array(\'hierarchical\' => true, \'label\' => \'Large Feature\', \'rewrite\' => false));
register_taxonomy(\'small_feature\', array(\'band_artists\'), array(\'hierarchical\' => true, \'label\' => \'Small Feature\', \'rewrite\' => false));
}
// Add custom taxonomies to Admin "Post List" Page
add_filter( \'manage_edit-band_artists_columns\', \'my_edit_band_artists_columns\' ) ;
function my_edit_band_artists_columns( $columns ) {
$columns = array(
\'cb\' => \'<input type="checkbox" />\',
\'title\' => __( \'Artists\' ),
\'large_feature\' => __( \'Large Feature\' ),
\'small_feature\' => __( \'Small Feature\' ),
\'date\' => __( \'Date\' )
);
return $columns;
}
// Make those columns display data
add_action( \'manage_band_artists_posts_custom_column\', \'my_manage_band_artists_columns\', 10, 2 );
function my_manage_band_artists_columns( $column, $post_id ) {
global $post;
switch( $column ) {
case \'large_feature\' :
$terms = get_the_terms( $post_id, \'large_feature\' );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $term ) {
$out[] = sprintf( \'<a href="%s">%s</a>\',
esc_url( add_query_arg( array( \'post_type\' => $post->post_type, \'large_feature\' => $term->slug ), \'edit.php\' ) ),
esc_html( sanitize_term_field( \'name\', $term->name, $term->term_id, \'large_feature\', \'display\' ) )
);
}
echo join( \', \', $out ); }
else { _e( \'\' ); }
break;
case \'small_feature\' :
$terms = get_the_terms( $post_id, \'small_feature\' );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $term ) {
$out[] = sprintf( \'<a href="%s">%s</a>\',
esc_url( add_query_arg( array( \'post_type\' => $post->post_type, \'small_feature\' => $term->slug ), \'edit.php\' ) ),
esc_html( sanitize_term_field( \'name\', $term->name, $term->term_id, \'small_feature\', \'display\' ) )
);
}
echo join( \', \', $out ); }
else { _e( \'\' ); }
break;
default :
break;
}
}
// Make those columns sortable
add_filter( \'manage_edit-band_artists_sortable_columns\', \'my_band_artists_sortable_columns\' );
function my_band_artists_sortable_columns( $columns ) {
$columns[\'large_feature\'] = \'large_feature\';
return $columns;
}
add_action( \'load-edit.php\', \'my_edit_band_artists_load\' );
function my_edit_band_artists_load() {
add_filter( \'request\', \'my_sort_band_artistss\' );
}
function my_sort_band_artistss( $vars ) {
if ( isset( $vars[\'post_type\'] ) && \'band_artists\' == $vars[\'post_type\'] ) {
if ( isset( $vars[\'orderby\'] ) && \'large_feature\' == $vars[\'orderby\'] ) {
$vars = array_merge(
$vars,
array(
\'meta_key\' => \'large_feature\',
\'orderby\' => \'meta_value\'
)
);
}
}
return $vars;
}