Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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