I’m working on a project where I’ve a dropdown of all the terms inside a taxonomy. If the user selects a term, then a function is executed. Eveything is working great, but I can’t get that dropdown orderded by name.

Do you have any idea of what the problem may be? I’ve a list outside a dropdown using a similar query and it works with no problem.

This is my query:

$args = array(
    'order'        => 'ASC',
    'orderby'      => 'name',
    'hide_empty'   => true,
    'cache_domain' => 'core'
);
$terms = get_terms('marcas', $args);
foreach ($terms as $item) {
    if ($item->count == 1) {
        $tax_query = '';
        $tax_query[] = array('taxonomy' => 'marcas','field' => 'term_id','terms' => $item->term_id);
        $term_post = get_posts(array('post_type' => 'prod','tax_query' => $tax_query));
        if (!empty($term_post)) {
            $term_post_link = get_permalink($term_post[0]->ID);
            $id_prod = url_to_postid($term_post_link);
            $nombre_prod = get_the_title($term_post[0]->ID);
            echo '<option value="'.$nombre_prod.'" href="'.$id_prod.'">'.$item->name.'</option>';
        }
    } else {
        echo '<option value="'.$item->term_id.'" label="'.$item->name.'">'.$item->name.'</option>';
    }
}

EDIT: SOLVED

I was working on someone else WP installation so I can get the info of WP Database to show Products information in a side-page. I’ve noticed that they have a plugin installed (Taxonomy order) that always return the terms posts in the same orden. Sorry for the confusion!

1 Answer
1

I was working on someone else WP installation so I can get the info of WP Database to show Products information in a side-page. I’ve noticed that they have a plugin installed (Taxonomy order) that always return the terms posts in the same orden. Sorry for the confusion!

Leave a Reply

Your email address will not be published. Required fields are marked *