WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 6 years ago

#26963 closed enhancement (invalid)

wp_get_object_terms using wrong orderby statement

Reported by: danielvonmitschke Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8
Component: Taxonomy Keywords:
Focuses: Cc:

Description

Tested on 3.8.1

In wp-includes/taxonomy.php it says:

Line 2002:

	if ( 'count' == $orderby )
		$orderby = 'tt.count';
	else if ( 'name' == $orderby )
		$orderby = 't.name';
	else if ( 'slug' == $orderby )
		$orderby = 't.slug';
	else if ( 'term_group' == $orderby )
		$orderby = 't.term_group';
	else if ( 'term_order' == $orderby )
		$orderby = 'tr.term_order';
	else if ( 'none' == $orderby ) {
		$orderby = '';
		$order = '';
	} else {
		$orderby = 't.term_id';
	}

Line: 2010

	else if ( 'term_order' == $orderby )
		$orderby = 'tr.term_order';

From my understanding the term_order == $orderby statement should be set on the terms table and not on the term_relationsships table (identical to the other orderby statements).

	else if ( 'term_order' == $orderby )
		$orderby = 't.term_order';

Change History (6)

#1 @bcworkz
8 years ago

  • Keywords reporter-feedback added

The 'term_order' column only appears on the term relationships table. There is no such column on the terms table, 't.term_order' is an invalid reference.

The default taxonomies do not use term_order AFAIK. Custom taxonomies can use it to store the order in which terms are added to objects. The term_relationships table is the only table that contains both term and object references, so it's logical for the term_order to be on the same table.

That much should be clear, perhaps you did not explain your concern well. Care to try again?

#2 @danielvonmitschke
8 years ago

  • Keywords reporter-feedback removed

Sorry, I just learned that the term_order column in the terms table was actually added by a plugin.

So I still was wondering why the ordering works with get_terms and not with wp_get_object_terms.
It seems that the plugin is using the 'get_terms_orderby' filter hook to alter the orderby-sql-statement in get_terms.
I think it would not hurt to add such a hook to wp_get_object_terms.

For instance:

$orderby = apply_filters('wp_get_object_terms_orderby', $orderby, $args, $taxonomies, $object_ids);

#3 @bcworkz
8 years ago

  • Type changed from defect (bug) to enhancement

I've no idea why one is filterable and one is not. I would guess get_terms() is a low level generic function where filterability is part of the function's design. wp_get_object_terms() is a more specialized function where plenty of reasonable options are provided, so there is little need for filters. The only columns that can't be ordered by are most of the IDs. Even then, the term ID is an available option.

Still, it would be easy to add a filter or two. If we put filters everywhere someone might possibly like to use one, WordPress would become rather bloated, so adding more filters should demonstrate it would benefit a reasonable number of people. I'm not making a judgment one way or the other here.

I am changing the ticket type from bug to enhancement though.

#4 @wonderboymusic
7 years ago

  • Keywords needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

This needs unit tests to demonstrate the issue

#5 @dipesh.kakadiya
6 years ago

Why term_order is in wp_term_relationships table.
Example
I have category 'term-A'
And i have two post post-X( id= 1 ) & post-Y( id = 2 ) which has assigned category 'term-A'

Then entry in db should like this in above case

term :
term_id | name | slug | term_group
1 | term-A | term-A | 0

term-taxonomy
term_taxonomy_id | term_id | taxonomy | description | parent | count
1 | 1 | category | description | | 0

term-relationship:
object_id | term_taxonomy_id | term_order
1 | 1 | 10
2 | 1 | 20

in above case for category term-A there are 2 term order 10 and 20

is it correct? As per my knowledge term_order column should be under term-taxonomy table.

Version 2, edited 6 years ago by dipesh.kakadiya (previous) (next) (diff)

#6 @boonebgorges
6 years ago

  • Keywords needs-patch needs-unit-tests removed
  • Milestone Future Release deleted
  • Resolution set to invalid
  • Status changed from new to closed

Sounds like the issue, as originally reported, is not valid. The wp_terms table does not have a term_order column.

I don't see the pressing need for a filter on the orderby parameter in wp_get_object_terms(). Such filters create an inconsistent developer interface, unless we add them everywhere; but if we add them everywhere, we introduce forward-compatibility issues. You can pass orderby in the function arguments, which seems like plenty to me.

@dipesh.kakadiya The column is there for historical reasons, and can't be moved. If there is a use case for such a column on wp_term_taxonomy, please open a ticket with the argument. Thanks!

Note: See TracTickets for help on using tickets.