0

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;
    }

1 Answer 1

1

The problem is 'meta_key' => 'large_feature', in your last function, here the "large_feature" is a taxonomy, not a "meta_key". Infact there is no post in wordpress which has "large_feature" as a "meta_key". That's the reason wordpress didn't display any posts.

About ordering by taxonomy terms, wordpress doesn't allow that like this. You can however customize the SQL query directly using posts_orderby or such filters, see http://codex.wordpress.org/Class_Reference/WP_Query#Filters

3
  • Okay, so it seems I have confused taxonomy slugs with category slugs. With regard to the other, though, I followed this tutorial which seemed to work for a host of other people. Did I just follow it incorrectly? justintadlock.com/archives/2011/06/27/… Nov 9, 2012 at 22:33
  • 1
    Or this tutorial only allows to sort by metadata and not any column data in the table? Nov 9, 2012 at 22:36
  • ...i think i just answered my own question here. Thanks blush Nov 9, 2012 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.