I want to create a URL which will redirect to the latest node of a specific content type so i write a custom module. In this example, the content type car is used.
latest_car.info
name = Latest car description = Create a URL which will redirect to the latest car node. package = Eureka; version = 7.x-1.0 core = 7.x
latest_car.module
<?php
/**
* Implements hook_menu()
*/
function latest_car_menu() {
$items = array();
$items['car'] = array(
'title' => t('Car'),
'page callback' => 'display_latest_car',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Redirect to latest car node
*/
function display_latest_car() {
// Get the latest node id of the car content type
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'car')
->propertyCondition('status', 1)
->propertyOrderBy('created', 'DESC')
->range(0, 1);
$result = $query->execute();
if (isset($result['node'])) {
// Rediret to the latest node page
drupal_goto(drupal_lookup_path('alias', "node/" . key($result['node'])));
} else {
// Redirect to front page if the query returns no result
drupal_set_message('Page not found', 'error');
drupal_goto('<front>');
}
}
Enable the module and then visit http://<drupal>/car, you should be redirect to the latest car node page.
Done =)

Hi,
Really interested in getting this going for my situation whereby the content type is called award_projects
Could you let me know what to replace with what?
Thanks in advance : )
Cameron
LikeLike
Replace the car with award_projects as follow.
<?php /** * Implements hook_menu() */ function latest_car_menu() { $items = array(); $items['car'] = array( 'title' => t('Car'), 'page callback' => 'display_latest_car', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK ); return $items; } /** * Redirect to latest car node */ function display_latest_car() { // Get the latest node id of the car content type $query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'car') ->propertyCondition('status', 1) ->propertyOrderBy('created', 'DESC') ->range(0, 1); $result = $query->execute(); if (isset($result['node'])) { // Rediret to the latest node page drupal_goto(drupal_lookup_path('alias', "node/" . key($result['node']))); } else { // Redirect to front page if the query returns no result drupal_set_message('Page not found', 'error'); drupal_goto('<front>'); } }LikeLike
This was amazing, exactly what i wanted, thank you so much. you saved me hours and hours of time
LikeLike
Good to know that i can help. =D
LikeLike
Hello,
after the page redirects to the latest node, the url is as follows: http:///node/nodeID. But is there a way to keep the url like this http:///car?
Thanks for the support.
LikeLike
A simple way to do that is to create an empty page using the Empty Page module. Then create a block in Views which will select the latest node and append this block to the empty page using Context.
But the drawback is you have to align the layout of this new page with node view page.
LikeLike