1

I want to sort tags list by tag id in my WordPress admin panel. There is 1000s tag in admin tag list and I want to know which are the latest added tags, but there are options to sort with Name, Description, Slug, and Coun but no option to sort by oldest or newest. Please tell me how can i add one more column "sort by ID".

1 Answer 1

1

Use orderby=id

You can simply sort the tags by term_id with the following GET request:

/wp-admin/edit-tags.php?taxonomy=post_tag&orderby=id&order=desc

and similar for other taxonomies.

Why does this work?

The orderby parameter is read through:

if ( !empty( $_REQUEST['orderby'] ) )
    $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );

in the WP_Terms_List_Table class and used as input argument in:

get_terms( $taxonomy, $args )

that is placed within the WP_Terms_List_Table::display_rows_or_placeholder() method.

This is how the orderby argument is handled in get_terms():

$_orderby = strtolower( $args['orderby'] );

if ( 'count' == $_orderby ) {
        $orderby = 'tt.count';
} elseif ( 'name' == $_orderby ) {
        $orderby = 't.name';
} elseif ( 'slug' == $_orderby ) {
        $orderby = 't.slug';
} elseif ( 'include' == $_orderby && ! empty( $args['include'] ) ) {
        $include = implode( ',', array_map( 'absint', $args['include'] ) );
        $orderby = "FIELD( t.term_id, $include )";
} elseif ( 'term_group' == $_orderby ) {
        $orderby = 't.term_group';
} elseif ( 'description' == $_orderby ) {
        $orderby = 'tt.description';
} elseif ( 'none' == $_orderby ) {
    $orderby = '';
} elseif ( empty($_orderby) || 'id' == $_orderby ) {
        $orderby = 't.term_id';
} else {
    $orderby = 't.name';
}

where we notice that the case with 'id' will be ordered by t.term_id, which is what we wanted.

Little puzzle

You might now think that using an empty orderby GET parameter, will also give the same ordering as 'id' because of this line:

} elseif ( empty($_orderby) || 'id' == $_orderby ) {

But that's not the case with the GET request. The reason is this condition (mentioned above):

if ( !empty( $_REQUEST['orderby'] ) )
    $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );

It will remove the empty orderby from the argument array and the default ordering in get_terms() will be used instead, i.e. ordering by name.

Your Answer

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

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