Skip to main content
Fixed values passed to node_load_multiple() in node_load_by_type()
Source Link

There is no such API for Drupal 6. The closest you can do is to properly query for all node ID for the content type then load each one using node_load() but this will require n+1 queries and is not very efficient.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $nodes = array();
  $query = db_rewrite_sql("SELECT nid FROM {node} n WHERE type = '%s'", 'n');
  $results = db_query_range($query, $type, $offset, $limit);
  while($nid = db_result($results)) {
    $nodes[] = node_load($nid);
  }
  return $nodes;
}

Note: db_rewrite_sql will ensure access check and other module provided filtering (such as language filtering provided by the i18n module).

For Drupal 7, you can use $nodes = node_load_multiple(array(), array('type' => $type));but the $conditions argument of node_load_multiple() is deprecated. Instead, you should use EntityFieldQuery to query for the node IDs then use node_load_multiple() but without a $conditions argument.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $type)
    ->range($offset, $limit);
  $results = $query->execute();
  return node_load_multiple(array_keys($results$results['node']));
}

There is no such API for Drupal 6. The closest you can do is to properly query for all node ID for the content type then load each one using node_load() but this will require n+1 queries and is not very efficient.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $nodes = array();
  $query = db_rewrite_sql("SELECT nid FROM {node} n WHERE type = '%s'", 'n');
  $results = db_query_range($query, $type, $offset, $limit);
  while($nid = db_result($results)) {
    $nodes[] = node_load($nid);
  }
  return $nodes;
}

Note: db_rewrite_sql will ensure access check and other module provided filtering (such as language filtering provided by the i18n module).

For Drupal 7, you can use $nodes = node_load_multiple(array(), array('type' => $type));but the $conditions argument of node_load_multiple() is deprecated. Instead, you should use EntityFieldQuery to query for the node IDs then use node_load_multiple() but without a $conditions argument.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $type)
    ->range($offset, $limit);
  $results = $query->execute();
  return node_load_multiple(array_keys($results));
}

There is no such API for Drupal 6. The closest you can do is to properly query for all node ID for the content type then load each one using node_load() but this will require n+1 queries and is not very efficient.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $nodes = array();
  $query = db_rewrite_sql("SELECT nid FROM {node} n WHERE type = '%s'", 'n');
  $results = db_query_range($query, $type, $offset, $limit);
  while($nid = db_result($results)) {
    $nodes[] = node_load($nid);
  }
  return $nodes;
}

Note: db_rewrite_sql will ensure access check and other module provided filtering (such as language filtering provided by the i18n module).

For Drupal 7, you can use $nodes = node_load_multiple(array(), array('type' => $type));but the $conditions argument of node_load_multiple() is deprecated. Instead, you should use EntityFieldQuery to query for the node IDs then use node_load_multiple() but without a $conditions argument.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $type)
    ->range($offset, $limit);
  $results = $query->execute();
  return node_load_multiple(array_keys($results['node']));
}
Post Migrated Here from stackoverflow.com (revisions)
Source Link
Pierre Buyle
  • 11.4k
  • 38
  • 63

There is no such API for Drupal 6. The closest you can do is to properly query for all node ID for the content type then load each one using node_load() but this will require n+1 queries and is not very efficient.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $nodes = array();
  $query = db_rewrite_sql("SELECT nid FROM {node} n WHERE type = '%s'", 'n');
  $results = db_query_range($query, $type, $offset, $limit);
  while($nid = db_result($results)) {
    $nodes[] = node_load($nid);
  }
  return $nodes;
}

Note: db_rewrite_sql will ensure access check and other module provided filtering (such as language filtering provided by the i18n module).

For Drupal 7, you can use $nodes = node_load_multiple(array(), array('type' => $type));but the $conditions argument of node_load_multiple() is deprecated. Instead, you should use EntityFieldQuery to query for the node IDs then use node_load_multiple() but without a $conditions argument.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $type)
    ->range($offset, $limit);
  $results = $query->execute();
  return node_load_multiple(array_keys($results));
}