node_get_recent() is deprecated in Drupal 10.1.x

ByKarthik Kumar D Kon9th May 2023, 2023-05-09T09:00:00+05:30
Read Article
Pause
Resume
Stop
node_get_recent() is deprecated in Drupal 10.1.x

node_get_recent() is unused in core and is now deprecated, developers can use views or EntityQuery to get the nodes which are recently modified or added.

Earlier drupal developers used to use the function node_get_recent() to get the recently changed nodes like seen in below example

$latest_updated_nodes = node_get_recent(10);

Now from Drupal 10.1.x onwards, developers need to use entity query to fetch the recently changed nodes like seen in below example

$nids = \Drupal::entityQuery('node')
  ->accessCheck(TRUE)
  ->condition('status', NodeInterface::PUBLISHED)
  ->sort('changed', 'DESC')
  ->range(0, 10)
  ->addTag('node_access')
  ->execute();
$latest_updated_nodes = !empty($nids) ? Node::loadMultiple($nids) : [];

Or Drupal Developers can use Views, where you create a View and add sort criteria with the latest updated date and fetch expected number of records.

Thanks for reading the article, for more drupal related articles read and subscribe to peoples blog articles.

We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies