I am loading terms programmatically but I am hoping to load them in the same order as they show in the CMS (weight). If not that, I just want them to load in alphabetical order.

This is what I am doing:

$vocabulary = taxonomy_vocabulary_machine_name_load($name);
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vocabulary->vid));

Any idea how to achieve that?

  • 1
    If you look at entity_load manual you will see the conditions array is depracated in favor of EntityFieldQuery, which should be able to achieve the sorting you want. – longboardnode Oct 27 '15 at 3:13

For getting all child terms in a vocabulary try the following code.

$vid= $tax->vid;
$child_terms = taxonomy_get_tree($vid);
foreach($child_terms as $values) {
  $cc[] = $tname; 

The above code will give you child terms of that vocabulary in array $cc. For sorting in alphabetical order use asort($cc);

Hope this will help you.


To expand on @longboardnode's comment, you can use EntityFieldQuery to retrieve the desired results set in the order that you specify.

You can then get the Term IDs from the results set, and pass that into entity_load() to get your term entities.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term');
$query->entityCondition('bundle', $machine_name);
$result = $query->execute();

$tids = array_keys($result['taxonomy_term']);
$terms = entity_load('taxonomy_term', $tids);

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.