Redirect by posted date

Last modified: July 10, 2009 - 16:54

Here's a snippet for redirecting to any page you want that Views returns. For instance, if you have March, April, and May newsletter drafts posted in March, April, and May, respectively, and you want to redirect your editors on login to the May newsletter draft -- then create a view called 'draft', filtering for newsletters, sorting descending by posted date so that the May newsletter is listed first, and returning a Node: Nid field, and enter the following code in your Login Destination settings:

<?php

global $user;
 
// Redirect the Administrator
 
if ($user->uid == 1) {
    return
'admin';
  }
 
// Redirect the Editor role
 
elseif ($user->roles[2]) {
   
// Load a view by name
   
$view = views_get_view('draft');
   
// Get an array of view result objects
   
$view->render();
    return (
'node/' . $view->result[0]->nid);
  }
 
// Redirect everyone else
 
else {
    return
'node';
  }
?>

$view->result[0]->nid returns the nid of the first newsletter in the results, which is May because you sorted descending by posted date.

 
 

Drupal is a registered trademark of Dries Buytaert.