I'm unable to access variables in template from preprocess function I am using a preprocess function to get data from specific content types. I am able to print the data in the pre-process function but unable to view the same in the twig file.

This is the code I am using.

    function themename_preprocess_node__custom_content_type_name(&amp;$variables) {
      $query = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;getQuery();
      // Get all products node IDs.
      $nids = $query-&gt;condition('type', 'content_type_products')-&gt;execute();
      // Load all product nodes.
      $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
      // Pass them to node--custom-content-type-name.html.twig.
      $variables['products'] = $nodes;
     //echo "&lt;pre&gt;";
     //print_r($variables['products']); exit;
    }


print_r($variables['products']) shows the content of $variables['products'], but the node--custom-content-type-name.html.twig file doesn't print anything for {{products}}.

Why?
You need to apply EntityViewBuilderInterface::viewMultiple before you can render the nodes in a twig template:

$variables['products'] = \Drupal::entityTypeManager()
  -&gt;getViewBuilder('node')
  -&gt;viewMultiple($nodes);


Twig:

{{ products }}


You can add a view mode to viewMultiple() as second argument, for example teaser.