• Home
  • About Mitch
  • Speaking
  • Articles
  • Contact
  • Home
  • About Mitch
  • Speaking
  • Articles
  • Contact

Digital Strategist

WordPress Developer

Content Creator

Unapologetic Punk

Mitch Canter

  • X
  • Bluesky
  • GitHub
  • Twitch
  • YouTube
  • LinkedIn
Tutorial

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

CMDR Mitchcraft

Reading time: 2 minutes

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.

  • Know Your Role: The Ultimate Guide to WordPress User Roles

    Know Your Role: The Ultimate Guide to WordPress User Roles

    Reading time: 3 minutes

    WordPress user roles play a critical role in managing access and permissions on a WordPress site. By assigning specific roles to a user, site owners can control what actions each user can perform on the site. WordPress User Roles There are six main roles included with any WordPress installation: Creating a User in WordPress Creating…

    WordPress
  • WP-Migrate-DB: Migrating a WordPress Installation in 5 Easy Steps

    WP-Migrate-DB: Migrating a WordPress Installation in 5 Easy Steps

    Reading time: 3 minutes

    If you’re a WordPress developer, migrating a finished development project can be one of the trickiest parts.  There’s moving parts in many places that have to be taken into consideration, and migration is (unfortunately) where WordPress actually could use some improvement.  However, it’s a necessary evil – one that I’ve done almost daily for a…

    WordPress