0

I've created custom column in my custom post type. This column shows the custom taxonomy. This is the code:

     function custom_MY_CUSTOM_POST_TYPE_column( $column, $post_id ) {
        switch ( $column ) {

            case 'MY_CUSTOM_COLUMN';
                $terms = get_the_term_list( $post_id , 'MY_CUSTOM_COLUMN' , '' , ',' , '' );
                if ( is_string( $terms ) )
                    echo $terms;
                else
                    _e( '-', 'your_text_domain' );
                break;    
        }
    }

Now I need this column to be sortable alphabetically so I created this code:

// Register the column as sortable
function x_column_register_sortable( $columns ) {
    $columns['MY_CUSTOM_COLUMN'] = 'x';

    return $columns;
}
add_filter( 'manage_edit-MY_CUSTOM_POST_TYPE_sortable_columns', 'x_column_register_sortable' );

but this isn't working. How can I makes this column works correctly?

0

You are trying to sort by "x" but WordPress doesn't know what that is. So you either need to sort by something WP understands like "title":

$columns['MY_CUSTOM_COLUMN'] = 'title';

Otherwise you need "x" to actually do something like such:

function my_custom_query_sort( $query ) {
    if ( ! is_admin() ) {
        return;
    }

    $orderby = $query->get( 'orderby');

    if ( 'x' == $orderby ) {
        $query->set( 'orderby', 'SOMETHING CUSTOM' );
    }

}
add_action( 'pre_get_posts', 'my_custom_query_sort' );

But that's not the only issue...You mention the column is a custom taxonomy. There isn't any core function in WordPress to sort posts by taxonomy. Not to mention that each post could have multiple terms assigned to them.

1
  • So correct me if I am wrong... There is no option to sort taxonomy alphabetically in column? – Damian P. Jul 27 '17 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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