WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 10 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:
PR Number:

Description

Hi,

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'				=> [
			'title',
			'editor',
			'thumbnail',
			'custom-fields',
		],
		'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 22 months ago.
Add missing documentation for 'sort' parameter
40436.1.diff (4.9 KB) - added by soulseekah 22 months ago.

Download all attachments as: .zip

Change History (9)

#1 @soulseekah
22 months ago

Duplicate #43705

Last edited 22 months ago by soulseekah (previous) (diff)

#2 @soulseekah
22 months ago

#43705 was marked as a duplicate.

@soulseekah
22 months ago

Add missing documentation for 'sort' parameter

@soulseekah
22 months ago

#3 @soulseekah
22 months 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
21 months 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 21 months ago by seanleavey (previous) (diff)

#5 @seanleavey
21 months 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
18 months ago

#43705 was marked as a duplicate.

#7 @GunGeekATX
10 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.