#custom post types, #php, #Plugins, #widget, #WordPress

Creating the Recent Custom Posts WordPress Widget

I recently found myself needing a way to display the most recent posts from multiple custom post types in multiple widgetized areas of a site I was working on.

Because I needed to do this quickly, and I assumed this need was common, I was on the lookout for an already well written and developed plugin that would do the job. I ran across a widget plugin from New2WP1 that allowed you to display the most recent posts from a single custom post type. Unfortunately, it wasn’t a multi-widget and you could only select one post type, so the plugin wasn’t suitable.

My first thought was that I could easily turn the plugin into a multi-widget capable plugin, but when I started to work on it, I realized that any modifications I made could easily be overwritten on the next update. Additionally, when I began work on turning it into a multi-widget plugin, it became exceedingly obvious that I would probably need to start from scratch.

That’s when I decided to develop a plugin of my own, based on the structure of what I saw in the plugin from New2WP, that would fit my needs, and hopefully the needs of others.

Where to Begin

Visit the Widgets API reference at the WordPress codex for a better overview of this information.

Once I figured out that I needed to turn the widget into one that could be used in multiple widgetized areas, I started to research like mad to find out what it would take to accomplish this. One trip over to the WordPress codex was enough to send me in the right direction.

It was basically as simple as this default usage code implies:

class My_Widget extends WP_Widget {
	function My_Widget() {
		// widget actual processes
	}

	function form($instance) {
		// outputs the options form on admin
	}

	function update($new_instance, $old_instance) {
		// processes widget options to be saved
	}

	function widget($args, $instance) {
		// outputs the content of the widget
	}

}
register_widget('My_Widget');

However, converting the existing plugin to this wasn’t as simple as the above code implies. The old plugin was built as a function, and its structure made it quite a bit more difficult to tie into the WordPress Widgets API.

Based on what I learned from New2WP’s plugin, and the WordPress Codex, I proceeded to build out my own plugin.

The Recent Custom Posts Plugin

I started by creating the directory /mm-recent-cpt/, the file mm-recent-cpt.php, and readme.txt file for future upload to the WordPress plugin directory.

In the php file I added the plugin header:

/**
 * Plugin Name: Recent Custom Posts
 * Plugin URI: http://mikemattner.com/custom-post-type-widget/
 * Description: Multi-widget for displaying recent posts of custom post types.
 * Version: 1.0
 * Author: Mike Mattner
 * Author URI: http://www.mikemattner.com/
 * Tags: custom post types, post types, latest posts, sidebar widget, plugin
 * License: GPL
 */

This allows WordPress to gather information about your plugin. If you just put that file into the plugins directory right now, you’ll have a plugin that does nothing, but it will show up in the list.

The structure of this widget is fairly simple because of the Widgets API. You’ll create a class that extends the WP_Widget class, essentially making it a breeze to create this plugin. I named my class mm_recent_posts. In order to get things moving, you need to initialize the plugin. In most cases you’ll use add_action to get your class or function working.

The function I’m calling with add_action returns register_widget, which takes the $widget_class argument. According to the Codex, $widget_class is the name of a class that extends WP_Widget.

So, this is my basic structure:

add_action( 'widgets_init', 'init_mm_recent_posts' );
function init_mm_recent_posts() { return register_widget('mm_recent_posts'); }

class mm_recent_posts extends WP_Widget {
    /** constructor */
    function mm_recent_posts() {
       parent::WP_Widget( 'widget_recent_cpt', $name = 'Recent Custom Posts' );
    }

    function widget($args, $instance) {
       // outputs the content of the widget
    }

    function update($new_instance, $old_instance) {
       // processes widget options to be saved
    }
    
    function form($instance) {
       // outputs the options form on admin
    }

}

The above is fairly self-explanatory as far as what each function’s purpose is. The constructor function mm_recent_posts() is designed to give a little information about the widget. You could use something along these lines to get it up and running: parent::WP_Widget('my_widget_base', 'My Widget title', array('description' => 'Very simple widget example', 'class' => 'my-widget-class'));.

Pretty easy, right?

The Plugin

The plugin I created is fairly straightforward. It is essentially just an extension of the original Recent Posts widget that comes with WordPress by default–except that this doesn’t overwrite that.

If this looks like something you could use, feel free to download, use, and modify till you’re blue in the face.

Download From WordPress Plugin Directory

I’ve now uploaded the plugin to the WordPress plugin directory. I hope you’ll find it to be useful.

I’m working on getting this added to the WordPress plugin repository The plugin can also be downloaded from the WordPress plugin repository. If you have any questions, comments, or modifications, let me know in the comments. I can’t guarantee that this will work with all plugins, particularly the Custom Post Type UI plugin, but I will say that I’ve used it, and it works for me. I apologize if my code isn’t up to snuff.

Also, welcome Stumblers! Thanks for stopping by. Hopefully you’ll find my little plugin to be useful.


  1. This is a fine plugin, it just needs to be multi-widget capable to be more useful.

17 Responses to “Creating the Recent Custom Posts WordPress Widget”

  1. Woy

    September 27, 2011 @ 8:08 am

    worked for me out of 2 which were in conflict with other plugins. Thanks.

    Reply

  2. Nikki

    October 18, 2011 @ 10:59 pm

    is there a way to show the thumbnail of the post?

    Reply

    • Mike

      October 19, 2011 @ 12:28 am

      This plugin’s output could certainly be modified for your purposes. In fact one of the reasons I built this was because I needed the thumbnail to also be displayed on my custom post type. What did you have in mind?

      Reply

  3. Nikki

    October 19, 2011 @ 12:22 pm

    Well I’m no coder lol – But I ended up using a plugin that actually uses more code :/ and put a marquee to it – I prefer fade in/out but that was the best I could do

    Reply

  4. Nitin Reddy Katkam

    December 15, 2011 @ 1:17 am

    Hi Mike!

    I just gave your plugin a quick run and it works as advertised. I’m planning to roll out “recent chat excerpts” to my blog, and perhaps even status updates and would be using the custom post types feature of WordPress.

    Using your plugin made adding my recent custom posts a no-code process – I actually though I would have to build my own widget because I was creating a custom post type (a few other plugins create their own post types and don’t let me select a post type).

    BTW, I feel your pain in having to migrate between the old widget API and the new widget API. I had to port some of my old code across to the new API and it was essentially a re-write of the section where I set and retrieve the plugin options.

    Reply

  5. Adam

    January 12, 2012 @ 3:37 am

    Thanks! I’m looking forward to pulling it apart :)

    Reply

  6. javad

    March 19, 2012 @ 6:27 pm

    Hi… Thanks very goood!
    Can be used for commercial use? [For example use in wordpress theme in themeforest.net]

    Reply

  7. Asher

    November 19, 2012 @ 10:16 pm

    Hey, I love your plugin. I’m wondering, is there a way to randomize the list? Thanks!

    Reply

  8. Mike

    November 20, 2012 @ 10:52 am

    Asher, that is certainly possible. How comfortable are you with php?

    Reply

    • Asher

      November 21, 2012 @ 5:50 pm

      I don’t have any background in it but I have moved things around and edited with trial and error and a lot of googling. I tried to add this: shuffle($mlq);

      in here
      $mlq = new WP_Query(array( ‘post_type’ => $types, ‘showposts’ => $number ));
      if( $mlq->have_posts() ) :

      before the if but nothing seemed to happen.

      Reply

    • Mike

      November 22, 2012 @ 10:21 am

      What you’re working with there is the WP_Query object, which is a WordPress class. Visit the following to see what else you can do: http://codex.wordpress.org/Class_Reference/WP_Query

      To do what you want, try changing this line:
      $mlq = new WP_Query(array( ‘post_type’ => $types, ‘showposts’ => $number ));

      To this:
      $mlq = new WP_Query(array( ‘post_type’ => $types, ‘showposts’ => $number, 'orderby' => 'rand' ));

      Hope that’s what you’re after. I might have to add sorting as an option in the future.

      Reply

  9. Matt Childers

    June 9, 2013 @ 10:31 am

    Thanks Mike. I just using custom post types and your plugin is exactly what I was looking for.

    Reply

Leave a Reply