How do I print the term names associated with a node? I want to show the term names used in a node. My term field machine name is field_category.

$query = \Drupal::entityQuery('node');
$query-&gt;condition('status', 1);
$query-&gt;condition('type', 'content_type');
$entity_ids = $query-&gt;execute();
$nodes = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;loadMultiple($entity_ids);
foreach ($nodes as $node) {
   dpm($node);
}


dpm() prints the following.

stdClass Object(
[__CLASS__] =&gt; Drupal\node\Entity\Node
[in_preview] =&gt; 
[values] =&gt; Array
 [field_category] =&gt; Array
            (
                [x-default] =&gt; Array
                    (
                        [0] =&gt; Array
                            (
                                [target_id] =&gt; 4
                            )

                        [1] =&gt; Array
                            (
                                [target_id] =&gt; 2
                            )

                    )

            )


How can I print the term names as term-1,term-2?
You can do this:

foreach($nodes as $node) {
  $categories = $node-&gt;field_category-&gt;referencedEntities();
  foreach($categories as $category) {
    print_r($category-&gt;getName(). ",");
  }
  print("\n");
}