I am using Drupal 8 and I want to create related content, but the problem is that I can't figure it out, how I can get all node IDs(except current one) for a specific content type.

  • Why not use Views for this task? – oksana-c May 6 '16 at 13:17

This is a basic query that returns last 10 changed nodes (published) of type 'article'. Modify for your needs.

$query = \Drupal::entityQuery('node')
  ->condition('status', NODE_PUBLISHED)
  ->condition('type', 'article')
  ->range(0, 10)
  ->sort('changed', 'DESC');
$nids = $query->execute();
  • I think you need to add in a condition to exclude current node id. – J. Reynolds May 5 '16 at 5:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.