Skip to content

Allow terms to match the order in the include parameter#67

Closed
glebkema wants to merge 2 commits into
ColorlibHQ:masterfrom
glebkema:master
Closed

Allow terms to match the order in the include parameter#67
glebkema wants to merge 2 commits into
ColorlibHQ:masterfrom
glebkema:master

Conversation

@glebkema
Copy link
Copy Markdown
Contributor

@glebkema glebkema commented Feb 10, 2020

Fix #66

To fix the problem it's necessary to change the behavior of the plugin in two aspects.

1) scporder_get_terms_orderby()

This method is used with the filter get_terms_orderby and changes the $orderby on t.term_order.

$orderby = 't.term_order';
return $orderby;

To fix this, it's enough to add one more check:

if (isset($args['include']) && isset($args['orderby']) && 'include' === $args['orderby'])
	return $orderby;

2) scporder_get_object_terms()

This method is used with two filters get_terms and wp_get_object_terms and sorts the found terms by the term_order.

usort($terms, array($this, 'taxcmp'));
return $terms;

This situation is more complicated. To check the include option in an array of arguments, we need to pass the $args parameter to the function.

if (isset($args['include']) && isset($args['orderby']) && 'include' === $args['orderby'])
	return $terms;

But this parameter is located at different positions in the mentioned hooks:

apply_filters( 'get_terms', $terms, $taxonomies, $args, $term_query );
apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );

In this regard, I propose to add an auxiliary method. Its role is to invoke a neighboring method with another set of arguments:

public function scporder_get_object_terms($terms, $object_ids, $taxonomies, $args) {
    return $this->scporder_get_terms($terms, $taxonomies, $args);
}

public function scporder_get_terms($terms, $taxonomies, $args) {

And use a specific function for each hook:

add_filter('wp_get_object_terms', array($this, 'scporder_get_object_terms'), 10, 4);
add_filter('get_terms', array($this, 'scporder_get_terms'), 10, 3);

puikinsh added a commit that referenced this pull request Jun 2, 2026
…dering

- Fix post order being scrambled on MariaDB / MySQL 8 when menu_order is
  re-normalized after gaps appear: refresh() now re-numbers deterministically
  in PHP instead of via a MySQL user-variable (@row_number) ranking whose
  evaluation order is undefined on those databases. Props @alexgw and
  @sebastiencyr (#147, #119).
- Honor `orderby=include` in get_terms() / wp_get_object_terms() instead of
  overriding it with the custom term order; the term filters now receive $args.
  Props @glebkema (#67, #66).
- Apply custom term ordering when any queried taxonomy is sortable (not only the
  first) and keep the caller's orderby as a fallback tiebreaker. Props @goaround
  (#104).

No asset or data-format changes; fully backward compatible with 2.7.0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@puikinsh
Copy link
Copy Markdown
Member

puikinsh commented Jun 2, 2026

Thank you @glebkema — this was a thorough, well-reasoned write-up, and you were spot on. 🙏

orderby=include was indeed being overridden by the custom term order, and the root cause was exactly as you described: the object-terms callback didn't receive $args, and get_terms / wp_get_object_terms pass it at different argument positions.

Fixed in 2.7.1 (just released). The term filters now receive $args, and both scporder_get_terms_orderby() and the shared terms sorter bail out when orderby === 'include' — implemented along the lines you proposed (a thin per-hook callback that hands $args to the sorter). Verified end-to-end: an include => [4,7,5], orderby => 'include' query now returns 4,7,5 instead of being re-sorted.

You're credited in the changelog. Closing as resolved — much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ignore the order of terms in the include parameter

2 participants