Drupal 7 – Create a URL which display the latest node of a specific content type

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 =)

6 thoughts on “Drupal 7 – Create a URL which display the latest node of a specific content type”

  1. 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

    Like

    1. 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>');
        }
      }
      

      Like

    1. 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.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.