0

I'm trying to get all the terms of a custom taxonomy (ediciones) but ordered by two ACF fields: edicion_ano (year field) and edicion_numero (number field). First should order by year and next by the number field.

I understand from ACF (5.5.X) it's possible to use meta_query and I'm ussing version 5.5.14.

In the new version of ACF (5.5.x) now uses term meta and WP allows meta queries for terms, so these work-arounds should no longer be needed. (source)

Right now I'm using this code:

$args = array(
    'taxonomy' => 'ediciones',
    'hide_empty' => false,
    'meta_query' => array(
        'relation' => 'AND',
        'edicion_ano_param' => array(
            'key' => 'edicion_ano',
            'compare' => 'EXISTS',
        ),
        'edicion_numero_param' => array(
            'key' => 'edicion_numero',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => array( 
        'edicion_ano_param' => 'DESC',
        'edicion_numero_param' => 'DESC',
    ),
);

$ediciones = get_terms($args);

Using the same meta_query but with get_posts() it's working fine. Actually with get_terms() show me a warning but I don't know why: Warning: strtolower() expects parameter 1 to be string, array given in .../class-wp-term-query.php on line 824

If I try order just for one field works fine with this code:

$args = array(
    'taxonomy' => 'ediciones',
    'hide_empty' => false,
    'meta_key' => 'edicion_ano',
    'orderby' => 'meta_value',
    'order' => 'DESC',
);

$ediciones = get_terms($args);

But I need order by both fields. Any idea what I'm doing wrong or how can make this works how I need?

Thanks!

1

That is because get_terms() does not accept array type as a value for the 'orderby' parameter. The documentation says about this parameter:

'orderby'

(string) Field(s) to order terms by. Accepts term fields ('name', 'slug', 'term_group', 'term_id', 'id', 'description'), 'count' for term taxonomy count, 'include' to match the 'order' of the $include param, 'meta_value', 'meta_value_num', the value of $meta_key, the array keys of $meta_query, or 'none' to omit the ORDER BY clause. Defaults to 'name'.

You can see here the original documentation page

  • That's true @Picard, my mistake. So, if orderby doesn't accept array type and is only string, how could I order by two fields the get_terms()? I can't figure out how to do it, can you help me? Thanks! – Jabel Márquez Jun 4 '17 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.