如何使用 entityQuery 获取指定术语表的术语?

如何使用 entityQuery 获取指定术语表的术语?下面的代码报错 'type' not found

$tids = \Drupal::entityQuery('taxonomy_term')
  ->condition('type', 'category')
  ->execute();
2
0

1 个回答

type 改成 vid 就可以了

$tids = \Drupal::entityQuery('taxonomy_term')
  ->condition('vid', 'category')
  ->execute();

但 entityQuery 只获取术语 ID,还需要进行加载才能获得 terms 对象

$terms = \Drupal::entityTypeManager()
  ->getStorage('taxonomy_term')
  ->loadMultiple($tids);

也可以使用以下代码直接加载术语对象

$terms = \Drupal::entityTypeManager()
  ->getStorage('taxonomy_term')
  ->loadByProperties([
    'vid' => 'category',
  ]);
2
0
登录注册后添加答案