How to query random nodes in Drupal 6 without views?

I can print out last nodes but random nodes are the problem.


Not sure where you are doing this, but this would be one dull ax way of doing it:

$query=db_query('SELECT nid, RAND() as _random FROM {node} WHERE status = 1 ORDER BY _random ASC LIMIT 1');

and then you can do whatever you want with $result->nid, another dull ax:

drupal_goto('node/' . $result->nid);


// do whatever you want with it


| improve this answer | |
  • 2
    The SQL can just be "SELECT nid FROM {node} WHERE status = 1 ORDER BY RAND() LIMIT 1", unless you really need the random number back. – mpdonadio Oct 10 '12 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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