Skip to content

Conversation

glebkema
Copy link
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);

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
1 participant