50 Days to a Better Blog–Day 9: Resurrecting Old Content

Tutorial

This post is the ninth of an ongoing series entitled “50 Days to a Better WordPress Blog”.  During this time, Mitch will be providing small snippits of code, plugins, and things you can do to make your blog more attractive, attain new readers, and keep old ones coming back time and time again. You can see the entire series here.

I've always wanted to use this photo too - and it's sort of relevant, right? :)

I've always wanted to use this photo too - and it's sort of relevant, right? 🙂

Let’s face it – you’ve been blogging for a long time now.  You’ve got lots and lots of great content, but some of it can get buried in the shuffle.  You tend to have 3-5 posts that get a lot of content, but the rest are just sort of… there.  Freeloading and drinking your Google Juice.  Let’s put them to work!

Displaying Random Content

A simple function can call 5 posts from your entire database and display them.  Typically you’d only want the title, but if you want to add more information then it’s just a simple loop – add to it what you wish (and tweak this code to suit your needs!)

Drop this in your sidebar:

<li class=”widget widget_oldposts”>
<h4 class=”widgettitle”>Old Posts</h4>
<ul>
<?php $recentPosts = new WP_Query(); $recentPosts->query(‘showposts=5&orderby=rand’); ?> <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
</li>

The special part is the “orderby” in the query – that allows us to literally pull 5 random articles.

Displaying Random Relevant Content

Random is great, but not necessary relevant.  Let’s specify even further by pulling 5 random posts form the category you’re currently viewing.  Best used as “related articles” under a post, by the way.

<?php $category = get_the_category();  $relcat = $category[0]->cat_ID; ?>
<h4>Older Posts</h4>
<ul>
<?php $recentPosts = new WP_Query(); $recentPosts->query     (‘showposts=5&orderby=rand&cat=’ . $relcat); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

The variable calls the global post variable’s category ID, and then displays that as a query variable in the new loop we create.  This allows us to pull content based on the main category a post is inserted in (it will pull in only one category, so multi-category posts will only related to the first category (main category) chosen.