How to get content with their taxonomy term name and id?
Make a dynamic query like this:

$query = \Drupal::database()-&gt;select('node_field_data', 'nfd');
$query-&gt;condition('nfd.type', 'product');
$query-&gt;leftJoin('node__field_category', 'ft', 'ft.entity_id = nfd.nid');
$query-&gt;leftJoin('taxonomy_term_field_data', 'fd', 'fd.tid = ft.field_category_target_id');
$query-&gt;fields('fd', ['tid', 'name']);
$query-&gt;fields('nfd', ['nid']);
$results = $query-&gt;execute()-&gt;fetchAll();


Suppose the reference field taxonomy machine name is "node__field_category".
The result of the query will be node id , term name and term id.