Loop the (WordPress) Loop

Somewhat recently, a client asked to create a way for visitors to submit content and rank said content for a site he was creating, much like the sites found on the cheezburger.com network. The site is powered by WordPress, so I installed and configured TDO Mini Forms for visitors to submit stuff, and GD Star Rating for the rating system. The site would have the posts in chronological order on a page, and the front page would have content ordered by ranking.

In the end, the front page simply displays content by ranking, which is easily accomplished with a page template and query_posts() with the GD Star Rating plugin’s parameters. My first attempt was over-engineered a bit for the client’s liking and was scrapped for the former option, but I thought it was a rather clever way of displaying posts – placing the WordPress loop within a loop.

I realize this has been done before, but here’s my twist on it:

<?php if (have_posts()) :
$daysBack = '0';
while ( $daysBack >= -7 ) :
$dateRange = getdate(strtotime($daysBack . "days"));
query_posts('year=' .$dateRange["year"] .'&monthnum=' .$dateRange["mon"] .'&day=' .$dateRange["mday"] .'&gdsr_sort=thumbs&gdsr_order=desc&showposts=1' );
<?php while ( have_posts() ) : the_post(); ?>
Do loop stuff...
<?php endwhile; // End the loop. Whew. ?>
<?php $daysBack--; ?>
<?php endwhile; ?>

Basically what this does is takes a look back through the last 7 days worth of posts, selects the highest-rated from each day, and displays them. Neat, huh?

So you may ask, “what if the site becomes derelict or just doesn’t get new content after a while…what then smart guy? The page won’t display any posts!” Sheesh, lay off! I already thought of this:

<?php rewind_posts(); ?>
<?php query_posts('orderby=rand&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
Do loop stuff again!
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

Further down on the page, we display a random post. Now the front page will display at least one post, and at most 8 posts. Oh snap!

Of course, this isn’t a fully complete solution. We should modify the second loop so that it does not display a post already displayed above, to avoid duplicating content. Even though this is incomplete and was scrapped in favor of just showing posts by rank and completely ignoring chronology, I thought it was a neat solution and thought I would share for anyone who may be interested.

Upgrading a RAID1 array with Parted Magic

Over this past weekend, I was tasked with upgrading disk space on one of our servers, a Dell PowerEdge 860. It has SAS 5/iR, originally with 2 160GB SATA disks mirrored. The data partition was down to only a couple GB, so it was in bad need of an upgrade. A little over a year ago, I performed a similar upgrade on a Dell Precision 390 – that had 2 750GB disks mirrored which was upgraded to 1.5TB array. To conserve resources, I used the 750GB disks to upgrade the array on the PowerEdge, along with a copy of Parted Magic, an open-source partitioning tool.

In brief, here’s the steps I took to perform the upgrade:

  1. Perform a full backup and defragment the drive. This process could potentially hose all the data on the disks, so a backup is a must. This server is running Win 2k3, and NTFS partitions are happier being resized after being defragmented.
  2. Power down the server and remove the original disks – be sure to mark them, preferably taking note of the primary disk of the array.
  3. Install the new disks, boot into the controller bios and build the new array.
  4. Power down the machine again. Remove the secondary disk of the new array (mark it!) and install the primary disk of the old array on another channel. The PowerEdge had on-board SATA channels, so I hooked it up there.
  5. Boot into Parted Magic. I used a CD, but there are USB drive and PXE options.
  6. Start up G4L. Do a raw copy, and select your source and destination disks. The disks are listed in standard *nix nomenclature (e.g. /dev/sda) with the size in bytes. Start the clone process. This can take some time. In this case, it took about 1hr 15mins to clone the disk. When I did this earlier on the Precision workstation, it took about 12hrs.
  7. Shut down, remove the primary from the original array and restart, just to be sure everything works.
  8. Shut down again and reboot into Parted Magic. Run Gparted to resize the partition.
  9. Reboot. After resizing, Gparted marked the partition as dirty, so chkdsk will run to make sure nothing’s broken.
  10. If everything looks good, shut down one more time to install the secondary disk of the new array. Reboot and enjoy the added space.

After this process, the array will be degraded, and consequently the machine may be sluggish until the disks sync. Performing this upgrade on the PowerEdge 860 took about 3hrs from start to finish, which was much better than the 15hrs that it took for the Precision 390 a year ago. In the latter case, a 750GB disk was being cloned, with much of the time consumed with the cloning process. This process should work with just about any RAID1 configuration, though YMMV.

From WordPress to Twitter to Facebook and back again

I like to keep things simple, yet I like to be connected. However, with all the social networking options available, this can be time consuming to accomplish. Some software and services are available to help with this, and I came to find that the most obvious ones are either broken or don’t work as I would like.

Basically, I have 3 places to post things; here, a WordPress (well, technically a WordPress MU) blog, Facebook and Twitter. The latter two are excellent for keeping in touch with friends, acquaintances and colleagues. The former was put together as a place to share ‘more public’ things – e.g. solutions to problems that I may come across, bits of code, a resume, and other geeky predilections.  Now, I’m not so naive to believe that data that I post to social networking sites are any ‘less public’ than what I post here – to the contrary, such data is probably more likely to get indexed and used to incriminate me, but the nature of the small status updates are less likely to make sense out of context and, by the nature of the character limits, can’t be used (as easily) to express complex thoughts or explanations. However, I digress….

My point is I wanted to make it so that if I update one of the 3, the other 2 would reflect the change. Getting WordPress and Twitter to play nice was the easy part – Twitter Tools is a great plugin that handles this elegantly and is quite flexible to get it to fit your particular needs. I’ve set it up here so that whenever I create a post, a status update is posted on twitter. Also, whenever I tweet, it creates a post here; fortunately, the plugin is intelligent enough so that an endless loop of posts and tweets do not occur. You can even tweet from the backend, though it’s not as nice an interface, as it doesn’t count characters as you type like Twitter does. Now, there are other plugins that allow you to update both Facebook and Twitter at once, but I didn’t care for them as much; in the end, I opted to try and get Facebook and Twitter to talk to one another.

Facebook has an official app that they developed to tie in to Twitter, however it only works for Pages (businesses/organizations), not Profiles (real, actual people). The folks at Twitter cooked up an app to update Facebook each time you tweet, however I could not get it to work. Google only readily turned up one other Facebook app, but that one requires you to add a #fb hashtag whenever you want a tweet to update your Facebook status (annoying!). I finally came across Tweetpo.st which works exactly as the official Twitter app is supposed to, though it has the option to use #fb hashtags if you’re in to that sort of thing. Additionally, it can work with multiple profiles, pages and twitter accounts so you can link them as you wish; a very nice feature if you happen to be a page admin.

Basically, the flow of information goes from this site to Twitter to Facebook, or from Twitter to this site and Facebook. It would be nice if status updates in Facebook would be tweeted or posted here to complete the circle, though I am satisfied with how things work currently. If I didn’t care what anyone else was up to, I could just log on here to let everyone know what I was up to. Actually, that’s not entirely a bad idea – it would certainly cut down on all the Mafia Wars and Farmville updates cluttering my Facebook feed, not to mention the DM spam I get on Twitter.