I've got a few custom post types with different taxonomies and I have a function that adds certain categories to post list view in edit.php admin screen. Everything good there. I've also added a function that will allow the user to sort by those categories. This is where things go wrong. When you sort by name or date, everything is fine, but when you sort by the custom categories all the posts disappear and I can't figure out why.
Here's the code all the way back through the custom post type registration. The code in question is the very last block, but I figured people may need to see the whole thing in context
// 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;
}