Support » Fixing WordPress » Get_terms_args filter broken

  • Resolved lectest1

    (@lectest1)


    Pre-4.7, this worked to order categories (and other taxonomies) in admin views (at post.php, post-new.php, and the admin ‘Categories’ page, itself):

    
        // Order list of categories in admin views
        function my_sort_terms( $args ) {
                $args['taxonomies'] = array( 'category' );
                $args['order'] = 'DESC';
                $args['orderby'] = 'ID';
                return $args;
        }
        add_filter( 'get_terms_args', 'my_sort_terms', 10, 2 );
    

    However, when I upgraded to WP 4.7, it stopped working.
    I’ve deactivated plugins. I tested on a stripped-down test site on 4.6 and 4.7 – same result. 4.7 stops this from working. Neither ‘add_filter’ nor ‘apply_filters’ works.

    How to fix? OR, in 4.7, how can I order categories/taxonomies by id on the admin side?

    • This topic was modified 2 years, 4 months ago by  Jose Castaneda. Reason: code formatted using backticks
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi there!

    Are you getting any PHP errors? Only thing I see wrong with that code is the second parameter being passed to the filtering function.

    It would be

    
    function my_sort_terms( $args, $taxonomies ) {
    	// code here
    }
    add_filter( 'get_terms_args', 'my_sort_terms', 10, 2 );
    

    There are no related php errors recorded in the log. If I go back to 4.6, it works again.

    And, yes, I added back the $taxonomies, second parameter, in the first line.

    Does this shed any light on this problem, or is it unrelated?
    https://core.trac.wordpress.org/ticket/38589#comment:3

    • This reply was modified 2 years, 4 months ago by  lectest1.

    Solution: This was a small bug in the 4.7 version of wp-includes/class-wp-term-query.php. See this ticket for the resolution and fix: https://core.trac.wordpress.org/changeset/39625

    Thank you to @boonebgorges for time and help in tracing this – and for the correct function/code for ordering taxonomies for admin views (I’ve used the taxonomy ‘category’ here, but it works with a custom taxonomy, as well):

    // Order list of categories in admin views
    function my_sort_terms_new( $args, $taxonomies ) {
    if ( ( is_array( $args[‘taxonomy’] ) && in_array( ‘category’, $args[‘taxonomy’] ) ) || ‘category’ === $args[‘taxonomy’] ) {
    $args[‘orderby’] = ‘id’;
    $args[‘order’] = ‘DESC’;
    }
    return $args;
    }
    add_filter( ‘get_terms_args’, ‘my_sort_terms_new’, 10, 2 );

    • This reply was modified 2 years, 4 months ago by  lectest1.
    • This reply was modified 2 years, 4 months ago by  lectest1.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get_terms_args filter broken’ is closed to new replies.