2

I am using an EntityFieldQuery to return an array of taxonomy terms matching a provided custom field value. Using the example given in this question, I wrote a function that accepts the vocabulary, field name, and field value as arguments and returns an array of matching terms. That works perfectly, but what I really want to get back is just the integer values corresponding to the matching terms, not the array of terms.

The problem is, the data structure that's being returned looks like this:

Array
(
    [6] => stdClass Object
        (
            [tid] => 6
            [vocabulary_machine_name] => features_of_interest
        )

)

for one item, and for another:

Array
(
    [79] => stdClass Object
        (
            [tid] => 79
            [vocabulary_machine_name] => features_of_interest
        )

)

Because of this, I can't see how to isolate the [tid] value (6 or 79 in the examples). I'd like to be able to say

$int_tid = $terms[XXX]['tid'];

but I can't get my head around whatever syntax is needed.

I've pasted my function below in case it helps. Ideally I would like to loop through the array of items, isolate each term ID value as an integer, and push it onto an array which the function would then return.

  function get_tids_from_custom_field($vocabulary, $field, $value) {

    $vocab_machine_name = taxonomy_vocabulary_machine_name_load($vocabulary);
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'taxonomy_term')
      ->entityCondition('bundle', $vocabulary)
      ->propertyCondition('vid', $vocab_machine_name->vid)
      ->fieldCondition($field, 'value', $value, '=');

    $terms = $query->execute();         
    $int_ids = array();

    foreach ($terms as $term) {
      dpm($term);
      array_push($int_ids, $term);
    }
    return $int_ids;
 }

2 Answers 2

2

The standard(?) pattern to get ids from a query over a single entity type is to use array_keys(). Something like this:

function get_tids_from_custom_field($vocabulary, $field, $value) {
  $vocab_machine_name = taxonomy_vocabulary_machine_name_load($vocabulary);
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'taxonomy_term')
    ->entityCondition('bundle', $vocabulary)
    ->propertyCondition('vid', $vocab_machine_name->vid)
    ->fieldCondition($field, 'value', $value, '=');

  $results = $query->execute();         

  return !empty($results['taxonomy_term']) ? array_keys($results['taxonomy_term']) : array();
}

The results array is keyed by the entity ID.

0
1

Try this:

foreach ($terms as $term){
  foreach ($term as $key => $value){
    array_push($int_ids, $key);
  }
}

It's probably not the best solution if you only ever have one term in a field but it should do the trick.

2
  • Many thanks for this @Darvanen. I should have known :-). Your answer works perfectly and if I could accept it in addition to MPD's answer I would, but I've accepted MPD's since it seems to be a more standard way to do it.
    – Frank H.
    Commented Dec 16, 2014 at 20:35
  • 1
    It's all good, MPD's answer is better.
    – Darvanen
    Commented Dec 16, 2014 at 22:20

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.