Drupal 8 Query to get similar blog post

Here we are going to fetch similar blog by blog node id, so inside .theme file
( in theme root folder) you can add below code and fetch similar blog by “field_blog_tags” field in drupal 8

Drupal 8 Query to get similar blog post

   $valuesTags=$variables['node']->get('field_blog_tags')->getValue();
  foreach($valuesTags as $key=>$value)
   {
   
   $smilarprd[]=$value['target_id'];
   }

   $nids = \Drupal::entityQuery('node')->condition('type', 'blog_post')->condition('field_blog_tags', $smilarprd,'IN')->condition('nid', $Nid, '!=')->range(0, 3)->execute();
  $RelatedBlog =  \Drupal\node\Entity\Node::loadMultiple($nids);
 
 $variables['RelatedBlog'] = $RelatedBlog;
  

My Thought

Your email address will not be published. Required fields are marked *

Our Tool : hike percentage calculator