Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Semi-Randomized Created and Changed Dates for Nodes in Drupal 7

Parent Feed: 

Let’s say you have a mixed content-type view like an e-commerce site with different product types shown on the homepage, or a general ‘front page’ kind of view, and the client is adding content to the site prior to launch. Typically they will add one type of content to the site at a time. If the feed is ordered in descending date order, this leads to a monotonous display of one type of content followed by another.

There are many ways to get around this issue, with varying degrees of time involvement and success. At one end would be working out new dates and editing nodes one a time. This is easy if you only have half a dozen pieces of content, but it is obviously not going to work for larger numbers.

When, years ago, I first came up against an issue like this, creating photo galleries in Gallery2, I either set the order of photos to manual and readjusted them laboriously or set them to random. You can do the same in Drupal too, making a new “sort order” field and filling it with numbers manually or via a script.

You can also set content to sort by global:random – this is desirable for content that should be rotated when the page reloads, like testimonials, ads, or featured content. But it is very confusing for visitors trying to browse a feed sorted randomly as except for caching, there is no persistence to what they see. I have seen others use the Cache Actions module to partly get around this, forcing the same cached random view result to be used until a new node is added or another similar event.

It was one such particular case where I decided all these inelegant solutions just weren’t going to cut it and worked on a dedicated solution. It would also work well in cases of sites wishing to appear to have been established longer or more regularly active than reality.

The Script:

  • Changes the created and changed dates of nodes to fall with a random distribution between two given dates $date1 and $date2
  • Works on one specified content type at a time but can be set to do all content on the site at once by uncommenting one line

Usage:

  1. Create a blank file, paste in the script and save as myscript.php. Then upload to a web accessible directory on your webserver.
  2. To run the script, visit myserver.com/myscript.php with your web browser.
<?php

/* SEMI-RANDOM CREATED,MODIFIED DATES FOR NODES
Author: Miles Carter http://www.milesjcarter.co.uk/blog/

ALWAYS BACKUP YOUR DATABASE! */
   
// INSTRUCTIONS (3 Steps)
 
// 1. Edit your Drupal database details:
       
$db_drupal = mysqli_connect('localhost', 'db_user', 'db_pass', 'db_name');

// 2. Edit "WHERE node.type='article'" to the content type you wish to randomize

$query = "SELECT node.nid, created, changed, type FROM node
LEFT JOIN node_revision ON 'node.nid'='node_revsion.nid' WHERE node.type='article'"
;

// - OR: Uncomment below line to update all content rather than just one content type at a time

#$query = "SELECT node.nid, created, changed, type FROM node LEFT JOIN node_revision ON 'node.nid'='node_revsion.nid'";

$results = mysqli_query($db_drupal, $query);
if (!$results) {
echo '

Error doing script: '

.
         mysqli_error($db_drupal) . '';
}
while ($row = mysqli_fetch_assoc($results)) {
$theid = $row['nid'];
$type = $row['type'];

/* 3. Edit below dates to suit, typical usage shown as example
   $date1 = The earliest possible date that content may be set to
   $date2 = The latest possible date */

$date1 = strtotime('2012-01-01');
$date2 = strtotime('now');

$rand_date = mt_rand($date1, $date2);

// Update DB
 $query_insert = "UPDATE node SET created='$rand_date', changed='$rand_date' WHERE nid='$theid'";
                               
if (!mysqli_query($db_drupal, $query_insert)) {

echo '

Error doing script: '

.
         mysqli_error($db_drupal) . '';
}
else {  
echo "Record change attempted: $type $theid $rand_date
"
;  }
}

mysqli_close($db_drupal);
?>

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web