I am using a vocabulary to get the list of terms in it. The tids for the terms are 1, 2, 3, 4, 5. I have no hierarchy in my taxonomy. Now I would like to find whether 1 is the first term in the vocabulary, similarly to find whether 5 is the last term in the vocabulary.

Is there any API way of doing this?

  • But first counting how? Vocabularies are tree, so do you mean first in own branch & depth, or first overall, aka first with no depth at all? – Mołot Oct 11 '13 at 7:03
  • I have no hierarchy in my taxonomy. The only things that makes the difference between the terms is the weight. – Gladiator Oct 11 '13 at 7:05

First, you need to obtain one level of your taxonomy terms. Use either taxonomy_get_tree($vid, 0, 1); for top level ones, or taxonomy_get_children($tid) for children of a known element.

It should be sorted the way you want it, as both functions contain this in their code:


Now you can use standard PHP functions reset(array &$array) and end(array &$array) to get first and last object from that array for comparison. Note that this will move internal pointer around, so don't use them lightly on arrays you also use elsewhere already.

| improve this answer | |

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.