MJC SEO & Web Development Blog > Semi-Randomized Created and Changed Dates for Nodes in Drupal 7

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

Fix sort order problems with a run-once script to set dates for existing nodes to fall randomly between two defined dates, e.g ’01-01-2012′ and ‘now’

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 '<p>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 '<p>Error doing script: ' .
         mysqli_error($db_drupal) . '';
}
else {  
echo "Record change attempted: $type $theid $rand_date <br />";  }
}

mysqli_close($db_drupal);
?>

3 Comments

  1. Guest
    Posted December 10, 2012 at 7:59 pm | Permalink

    Thanks very much, I did not expect someone posted the solution on the web already. Works perfectly.

  2. Robert Gerard
    Posted February 22, 2013 at 9:22 pm | Permalink

    Great info…thanks for sharing!

    All the best,

  3. Posted February 24, 2013 at 12:06 pm | Permalink

    Thanks, this is so simple and effective.

Post a Comment

Your email is never shared. Required fields are marked *

*
*