Make WordPress Core

Opened 3 years ago

Last modified 18 months ago

#40436 new defect (bug)

Custom taxonomy terms order lost under wp-admin/post.php edit action for a custom post type

Reported by: solo14000 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: has-patch has-unit-tests
Focuses: administration Cc:



I have registered a custom taxonomy (for a custom post type) that supports sorting using the following taxonomy args

        const DEFAULT_ARGS = [
                'public'                                => true, // Make WP function is_tax() working for this custom taxonomy
                'show_ui'                               => true,
                'show_in_menu'                  => true,
                'show_in_nav_menus'             => false,
                'show_tagcloud'                 => false,
                'show_in_quick_edit'    => true,
                'meta_box_cb'                   => null, // If null default to WP post_tags_meta_box() in not 'hierarchical' otherwise to post_categories_meta_box() (see WP meta-boxes.php)
                'show_admin_column'             => true,
                'hierarchical'                  => false,
// 2017-01-25 Seems not to be necessary anymore         'update_count_callback' => 'My_Query_Admin::cb_update_count_callback',
                'query_var'                             => true, // Make WP function is_tax() working for this custom taxonomy
                'rewrite'                               => false,
                'capabilities'                  => [],
                'sort'                                  => true, // Need by post terms ordering for M_Post_Type::get_main_term() (https://core.trac.wordpress.org/ticket/5857)
                '_builtin'                              => false,               

I used the following args for registering the custom post type

	// Default custom post type arguments
	//	https://codex.wordpress.org/Function_Reference/register_post_type
	const DEFAULT_ARGS = [
 		'description'			=> '',	
		'public'				=> false,
		'exclude_from_search'	=> true,
		'publicly_queryable'	=> true,
		'show_ui'				=> true,
		'show_in_nav_menus'		=> false,
		'show_in_menu'			=> true,
		'show_in_admin_bar'		=> true,
//		'capability_type'		=> 'post',		
//		'capabilities'			=> [],
//		'map_meta_cap'			=> false,
		'hierarchical'			=> false,
		'supports'				=> [
		'register_meta_box_cb'	=> null,
		'has_archive'			=> false,
		'rewrite'				=> false,
		'permalink_epmask'		=> EP_PERMALINK,
		'query_var'				=> true,
		'can_export'			=> false,
		'delete_with_user'		=> false,
		'show_in_rest'			=> false,
//		'rest_base'				=> $post_type,
//		'rest_controller_class'	=> WP_REST_Posts_Controller,	
//		'_builtin'				=> false,
		'_edit_link'			=> 'post.php?post=%d',

Terms order is well respected using

wp_get_object_terms($post->ID, $taxonomy, ['orderby' => 'term_order']);

but when editing post under admin panel, order is lost and reverts always to alphabetical order.

Can anyone confirm that bug or tell me what's wrong for me?

Thanks a lot

Attachments (2)

40436.docs.diff (1.6 KB) - added by soulseekah 2 years ago.
Add missing documentation for 'sort' parameter
40436.1.diff (4.9 KB) - added by soulseekah 2 years ago.

Download all attachments as: .zip

Change History (9)

#1 @soulseekah
2 years ago

Duplicate #43705

Last edited 2 years ago by soulseekah (previous) (diff)

#2 @soulseekah
2 years ago

#43705 was marked as a duplicate.

2 years ago

Add missing documentation for 'sort' parameter

2 years ago

#3 @soulseekah
2 years ago

  • Keywords has-patch has-unit-tests added

Wow this was a tough one! Adding default sort order for taxonomies with the set sort flag was straightforward. But then update_object_term_cache popped up. What it did was fetch all the taxonomy terms for an object ordering them by name. Had to split this logic into two parts, one for fetching name-sorted taxonomies, one for fetching term_order-sorted taxonomies.

Distantly related to #41679

#4 @seanleavey
2 years ago

Hi @soulseekah, thanks so much for working on this patch! This is a feature I'd really love to see in WordPress as I currently have to requery the terms using the get_the_terms filter to make WordPress respect the order in which terms were defined in the post edit screen.

This issue recently cropped up again for me (https://github.com/WordPress/gutenberg/issues/6688) in Gutenberg when I was trying to display terms in term_order in a non-hierarchical taxonomy meta box. Overriding get_the_terms fixes the order, but, as I said, this requires a second query that should ideally not be necessary.

Any idea on when this could be merged in to core?

Last edited 2 years ago by seanleavey (previous) (diff)

#5 @seanleavey
2 years ago

I just discovered that adding an args index to the $args array in the register_taxonomy call itself containing an array with the default order set to term_order gives me what I want, as per the procedure presented in #40496. This means I don't need to filter get_the_terms to change the order, but the use of the args parameter is undocumented.

In any case, it would be great if both the sort and args parameters in register_taxonomy could be documented to show they are officially supported.

#6 @netweb
2 years ago

#43705 was marked as a duplicate.

#7 @GunGeekATX
18 months ago

Ran into what's probably the same issue today, setting the term order, then calling wp_update_post is resetting the order to alphabetical. Sample code and output: https://gist.github.com/petenelson/7feef670cf03d87376620adc3ee31861

Note: See TracTickets for help on using tickets.