wp-term-order icon indicating copy to clipboard operation
wp-term-order copied to clipboard

`wp_term_order_taxonomy_override_orderby_supported` filter prevents explicitly ordering by `order`

Open ethanclevenger91 opened this issue 1 year ago • 1 comments

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');
}

ethanclevenger91 avatar Aug 14 '23 22:08 ethanclevenger91

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 avatar Mar 10 '24 20:03 JJJ