Support » Developing with WordPress » filter get_terms_args and orderby meta_value_num not working

  • Hi,

    this is working :

    add_filter( 'get_terms_defaults', 'my_get_terms_defaults', 10, 2 );
    function my_get_terms_defaults( $defaults, $taxonomies ) {
    	if ( in_array( 'my_tax_types', $taxonomies ) ) {
    		$defaults['orderby']		= 'meta_value_num';
    		$defaults['meta_key']	= 'tax_position';
    	}
    	return $defaults;
    }

    this is working too (for example) :

    add_filter( 'get_terms_args', 'my_get_terms_args', 10, 2 );
    function my_get_terms_args( $args, $taxonomies ) {
    	if ( in_array( 'my_tax_types', $taxonomies ) ) {
    		$args['orderby']	= 'name';
    		$args['order']		= 'DESC';
    	}
    	return $args;
    }

    BUT this is NOT working :

    add_filter( 'get_terms_args', 'my_get_terms_args', 10, 2 );
    function my_get_terms_args( $args, $taxonomies ) {
    	if ( in_array( 'my_tax_types', $taxonomies ) ) {
    		$args['orderby']	= 'meta_value_num';
    		$args['meta_key']	= 'tax_position';
    	}
    	return $args;
    }

    Please someone can tell me why the filter get_terms_args isn’t working with orderby with meta_value ?…
    Here https://core.trac.wordpress.org/ticket/34996, Boone Gorges said it was fixed. But after 3 days testing i can make it working !

    • This topic was modified 2 years, 1 month ago by Steve Stern. Reason: put code in backticks
    • This topic was modified 2 years, 1 month ago by Steve Stern.
    • This topic was modified 2 years, 1 month ago by Steve Stern.
Viewing 4 replies - 1 through 4 (of 4 total)
  • I think this functionality may be broken again and the ticket may need to be re-addressed.

    That’s what i think too. But how do you do that (i new here)

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    The get_terms_args filter happens after the meta query is setup, so you cannot order by term meta at that point, because it’s too late in the process.

    You can order by term meta by passing the correct values to get_terms, or by filtering get_terms_defaults. Either of these work.

    Here’s the function that gets the terms:

    https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-term-query.php#L287

    First it parses the query. The defaults filter is in that function, to define the defaults used to parse against.

    Next, the meta query is constructed with the resulting returned arguments. This is so it can be available to the pre_get_terms action.

    The get_terms_args filter isn’t processed until down on line 353. Since the meta query is already constructed, then that’s too late to change the arguments which might affect said query.

    It may be that this filter needs to be moved earlier in the code. You should make a new ticket for that.

    Thank you Samuel for your very complete answer.
    I will try to open a ticket.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘filter get_terms_args and orderby meta_value_num not working’ is closed to new replies.