Mario. Front End Web Developer, Snowboarding Addict

Drupal: get nodes by type

It appears that if you’re trying to get nodes by type in your Drupal module or theme (i.e. not on a page, in which case you should use Views) then the only way to get them is through an SQL query.

// get nid, title fields for all nodes of type film
$results = db_query('SELECT nid, title FROM {node} WHERE type = "%s"', 'film');
// get all fields from anything of type cinema
$results = db_query('SELECT * FROM {content_type_cinema}');

Not as scary as you might think. Use Drupal’s db_fetch_object() to iterate through your results.

while ($node = db_fetch_object($results)) {
	// take a looksie at what we've got
	var_dump($node);
}

There is also Drupal’s node_get_types() function that will return an array with the machine name of each of the content types on your site.

Be the first to comment!