2

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?

2
  • 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
    Commented Oct 11, 2013 at 7:03
  • I have no hierarchy in my taxonomy. The only things that makes the difference between the terms is the weight.
    – Gladiator
    Commented Oct 11, 2013 at 7:05

1 Answer 1

5

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:

$query->orderBy('t.weight');
$query->orderBy('t.name');

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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