stuttter / wp-term-order

Sort taxonomy terms, your way
https://wordpress.org/plugins/wp-term-order/
GNU General Public License v2.0
95 stars 24 forks source link

Can't order custom term by name #11

Closed martinfojtik closed 2 years ago

martinfojtik commented 7 years ago

My code is:

$args = array (
  'taxonomy'               => array( 'country' ),
  'hide_empty'            => false,
  'order'                      => 'ASC',
  'orderby'                  => 'name',
);
$term_query = new WP_Term_Query( $args );
reubenlillie commented 7 years ago

@JJJ, I'm wondering if this isn't related to #4 and/or what @cclass posted in the WP Term Order Support Forum.

I also have a custom taxonomy that I'm trying to list alphabetically by name (default WordPress behavior, right?) with the List Custom Taxonomy Widget by @celloexpressions. However, with WP Term Order activated (which I rely on heavily for another custom taxonomy), the order seems to be affected by some of the quick edits I've made and perhaps the order in which I entered/edited some of the terms.

After checking my database, the order column in the wp_term_taxonomy table reads 0 from top to bottom, but the actual order in which terms are listed—in the widget, the database, and the WordPress admin—resembles some #4-like bug.

I also suspect that this sort of behavior is part of the reason why some people would like to choose the taxonomies to which WP Term Order applies (e.g., #10). (Sure, it would also be nice to have a setting to help clients and developers alike to protect themselves/ourselves from changing the order of taxonomies that we/they shouldn't be reordering, whether inadvertently or intentionally.)

But, I'll confess, I'm not sure how to implement the wp_term_order_get_taxonomies filter on L269. The method private static function get_taxonomies( $args- array() ) on L258 is such a central and specialized operation.

An example of proper filtering would be nice.

Even then, I'm not too keen on having to use such a filter just to be able to use "default" WordPress functionality for a taxonomy that I have not otherwise altered explicitly with WP Term Order.

So a setting like what @stevengliebe suggested in WP Term Order Support might be even better.

Still, even without a setting, something like the List Custom Taxonomy Widget should still work as expected when WP Term Order is activated.

@martinfojtik, @benhuson, @cclass, @stevengliebe, @phh, @JJJ please forgive me if mine is a separate issue. I was trying to avoid creating a duplicate of what seems to be an instance of a similar issue for several of us.

stefanfisk commented 5 years ago

I believe that the root cause is this if, where t.name is always changed to tt.order.

elseif ( 't.name' === $orderby ) will never apply due to in_array( $orderby, array( 'name', 't.name' ) )

https://github.com/stuttter/wp-term-order/blob/d155c9c975baa15c443d4c8d973e405abe24ff2a/wp-term-order.php#L518-L523

benhuson commented 5 years ago

@stefanfisk Yes, I tracked it back to that too, it is always overridden by custom order at that point.

I recently created a proxy get_terms() function to get around this. It's not elegant and I would rather there was a way for the plugin to respect ordering by name if specified (or at least provide a way to pass a parameter to force this), but in the meantime, I did this:

function mytheme_get_terms( $args = null ) {
    add_filter( 'get_terms_orderby','mytheme_get_terms_order_fix', 20, 2 );
    $terms = get_terms( $args );
    remove_filter( 'get_terms_orderby','mytheme_get_terms_order_fix', 20 );
    return $terms;
}

function mytheme_get_terms_order_fix( $orderby = 'name', $args = array() ) {
    if ( 'name' == $args['orderby'] && $orderby !== 't.name' ) {
        $orderby = 't.name';
    }
    return $orderby;
}
JJJ commented 2 years ago

Pushed a fix to master. Give it a try, and report back? <3