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

wp_term_order_taxonomy_override_orderby_supported filter prevents explicitly ordering by order #26

Open
ethanclevenger91 opened this issue Aug 14, 2023 · 1 comment
Assignees
Milestone

Comments

@ethanclevenger91
Copy link

ethanclevenger91 commented Aug 14, 2023

I'm imagining a scenario where this plugin is installed and functional, but a developer may want opt-in behavior for the actual sorting of terms.

If the wp_term_order_taxonomy_override_orderby_supported filter is used to return false for a taxonomy, there is no way to get those terms in the ordered order. The following will not get short-circuited:

get_terms([
    'taxonomy' => 'my-tax',
    'orderby' => 'order',
]);

If that filter is not utilized, and the default behavior is used, the following will sort by the custom order before sorting by name, rather than sort by just name:

get_terms([
    'taxonomy' => 'my-tax',
    'orderby' => 'name',
]);

The solution seems to by that you would either respect name when explicitly set as the desired order, or the filter above would have a conditional for when order has been explicitly set as the desired order.

My current workaround looks like this:

if('name' == $args['orderby']) {
    add_filter('wp_term_order_taxonomy_override_orderby_supported', '__return_false');
}

$product_cats = get_terms($args);

if('name' == $args['orderby']) {
    remove_filter('wp_term_order_taxonomy_override_orderby_supported', '__return_false');
}
@JJJ
Copy link
Contributor

JJJ commented Mar 10, 2024

Hey @ethanclevenger91! 👋

The reason I went heavy-handed with the wp_term_order_taxonomy_override_orderby_supported filter, is because WordPress almost always explicitly passes name as the orderby.

(This is particularly problematic in the Terms Admin Screens where WordPress forces name but we need to force our own custom one just to make the interface of the plugin function as intended in the first place.)

For example, if you search for wp_dropdown_categories() usages in wp-admin each of them has name explicitly set as the orderby, and there is no way to know if it is a default argument for the plugin to override or an override to the plugin back to name again.

The best compromise I could muster is to have other filters in place (wp_term_order_taxonomy_supported and wp_term_order_get_taxonomies) to allow users some global interception point where other decisions could be made.


Lastly, this plugin was released before Term Meta as an API had been added into WordPress Core, which is partially why it doesn't use it by default – the other part is that I just think the extra database column is ultimately the most performant approach, even if it is somewhat invasive. 😅


It is plausible to refactor some of this plugin's code to drop support for the additional database column, only use meta, and use meta_value_num as the orderby instead, though I'm not 100% certain that approach will help you in your implementation, so I'm curious to hear more about that from you! 👍

@JJJ JJJ added this to the 3.0.0 milestone Mar 10, 2024
@JJJ JJJ self-assigned this Mar 10, 2024
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

No branches or pull requests

2 participants