WordPress.org

Make WordPress Core

Opened 8 years ago

Last modified 8 months ago

#23421 assigned enhancement

Add sortable to taxonomy column

Reported by: tifosi Owned by: swissspidy
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch dev-feedback 2nd-opinion
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Following on from the #21240 ticket which introduced the show_admin_column functionality one limitation is the 'as easy' way to make that column sortable.

It's possible to do it through the 'manage_edit-{$post_type}_sortable_columns' filter but the column_key is different for 3.5+. A standardisation as for adding the column may be advantageous: a register_taxonomy argument and / or new filter.

argument: show_column_sortable, boolean default false, requires show_admin_column to be set and true.

In the same way that the get_columns function uses the new "manage_taxonomies_for_{$post_type}_columns", filter to add a taxonomy column there could be a "manage_taxonomies_for_{$post_type}_sortable_columns" filter in the get_sortable_columns function of the post-list class:

i.e.

function get_sortable_columns() {
	$default = array(
		'title'    => 'title',
		'parent'   => 'parent',
		'comments' => 'comment_count',
		'date'     => array( 'date', true )
	);

	return apply_filters( "manage_taxonomies_for_{$post_type}_sortable_columns", $default );
}

The developer could then set the filter as normal

add_filter( "manage_taxonomies_for_{$post_type}_columns", 'add_sortable_column' );

function add_sortable_column ( $columns ) {
	$columns[ taxonomy-tax_name ] = 'Taxonomy Label';
	return $columns;
}

Attachments (4)

23421.diff (4.8 KB) - added by jtsternberg 6 years ago.
Allow sortable columns via the show_column_sortable taxonomy argument.
23421.2.diff (9.8 KB) - added by cr0ybot 8 months ago.
Fixed posts dropping out of the ordered list if no taxonomy term is applied.
23421.3.diff (8.6 KB) - added by cr0ybot 8 months ago.
Fixed patch file of 23421.2
23421.4.diff (10.8 KB) - added by cr0ybot 8 months ago.
Fix typo & comment cleanup

Download all attachments as: .zip

Change History (21)

#1 @SergeyBiryukov
8 years ago

  • Description modified (diff)
  • Type changed from feature request to enhancement

#2 @SergeyBiryukov
8 years ago

  • Summary changed from Follow on from #21240. Add sortable to taxonomy column to Add sortable to taxonomy column

#3 @tifosi
8 years ago

Of course above

add_filter( "manage_taxonomies_for_{$post_type}_columns", 'add_sortable_column' );

should be new filter

add_filter( "manage_taxonomies_for_{$post_type}_sortable_columns", 'add_sortable_column' );

cut n paste error.

#4 follow-up: @wonderboymusic
7 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 4.0

I actually assumed this was already there - making this work with SQL and a filter sucks (I have written some nasty SQL in my own projects). I will investigate this in 4.0, seems like it might be easy now.

#5 in reply to: ↑ 4 @jtsternberg
6 years ago

Replying to wonderboymusic:

I actually assumed this was already there - making this work with SQL and a filter sucks (I have written some nasty SQL in my own projects). I will investigate this in 4.0, seems like it might be easy now.

http://scribu.net/wordpress/sortable-taxonomy-columns.html
http://wordpress.stackexchange.com/questions/109955/custom-table-column-sortable-by-taxonomy-query
I can take a stab at this if you'd like.

#6 @wonderboymusic
6 years ago

Bring it.

@jtsternberg
6 years ago

Allow sortable columns via the show_column_sortable taxonomy argument.

#7 @jtsternberg
6 years ago

  • Keywords has-patch added; needs-patch removed

23421.diff Uses SQL and a filter, so not sure it's the best way to handle it in the long-run, but I think this is a decent place to start. I set the default for categories/tags to be true for sortable, but I'm not sure that's what we want.

#8 @jtsternberg
6 years ago

  • Keywords dev-feedback 2nd-opinion added

This ticket was mentioned in IRC in #wordpress-dev by helen. View the logs.


6 years ago

#10 @helen
6 years ago

  • Keywords 2nd-opinion removed
  • Milestone changed from 4.0 to Future Release

Bit late for this for 4.0.

#11 @johnbillion
6 years ago

  • Owner set to johnbillion
  • Status changed from new to accepted

FWIW I use the same code from Scribu in a library which we use on many of our client sites. I'll get this into 4.1.

#12 @wonderboymusic
5 years ago

  • Keywords dev-feedback removed

@johnbillion: bump

#13 @tifosi
4 years ago

@johnbillion Bump

One thing to note is that scribu's sql - which has become the defacto standard when googling for this - does a join on the terms table. The resultant output thereby strips all post records where the term is null. This may or may not be the required result.

Still, would like this to make it into 4.8 as an additional arg in 'register_taxonomy'

show_admin_sortable

Also a useful partner argument:

show_admin_filter

To enable a drilldown select to show in the options above the table. Both together would make an excellent triumvirate for taxonomy functionality.

Thanks

#14 @swissspidy
4 years ago

  • Owner changed from johnbillion to swissspidy
  • Status changed from accepted to assigned

I'll try to wrap my head around this to come up with a refreshed patch.

@cr0ybot
8 months ago

Fixed posts dropping out of the ordered list if no taxonomy term is applied.

#15 @cr0ybot
8 months ago

  • Keywords dev-feedback 2nd-opinion added

I took a crack at this in 23421.2.diff—it's a more complicated feature than it seems on the face of it. Most notably, ordering by a taxonomy column, where multiple terms may be present on a single post, actually means ordering by the plain-text list of terms concatenated. So any posts with just "Term A" applied will be grouped, and then a post with "Term A, Term B" will come after.

The biggest change to the SQL was to put the taxonomy table joins into a subselect so that we don't drop posts without any terms.

After all this effort, I wonder if orderby on a taxonomy column even makes sense. It seems as though a more generic taxonomy filter (as suggested by @tifosi as a show_admin_filter option) would provide more predictable or expected results.

I used most of what @jtsternberg had, though I moved most of the functionality into the WP_Posts_List_Table since that seemed to be the precedence set by the code for show_admin_column. I also added the setting to the WP_Taxonomy class, and the posts_clauses filter is applied in admin-filters.php.

NOTE: I accidentally included another commit, I'm attempting to clean it up now...

Version 1, edited 8 months ago by cr0ybot (previous) (next) (diff)

@cr0ybot
8 months ago

Fixed patch file of 23421.2

#16 follow-up: @jtsternberg
8 months ago

Of note, you're new patch contains the same typo as in my patch ha ha, peform vs perform.

#17 in reply to: ↑ 16 @cr0ybot
8 months ago

Replying to jtsternberg:

Of note, you're new patch contains the same typo as in my patch ha ha, peform vs perform.

Ahahaha, how did I not catch that? Oh well, this gave me an opportunity to remove an errant comment.

@cr0ybot
8 months ago

Fix typo & comment cleanup

Note: See TracTickets for help on using tickets.