I’m able to easily include my custom post types into my main loop by making small adjustments with query_posts()
, but I’m not sure how I would go about including custom post types in the “Recent Posts” sidebar widget (or any of the other widgets, for that matter).
How should I go about expanding “Recent Posts” scope to include more than just the native post type?
You’ll have to edit the code for the Recent Posts widget or create your own version based on the default. The code is in the
wp-includes/default-widgets.php
file around line 513. But since you should never make modifications to core, my recommendation would be to copy the code to create your own My Custom Recent Posts widget and use that on your site. Just drop the new widget class into your theme’sfunctions.php
file or use it in a plugin.The only real modification you need to make are to the widget’s class name and encapuslated functions and options (so that there aren’t any naming conflicts with the original Recent Posts widget. After that, you’ll need to edit the call to
WP_Query
in thewidget()
constructor so that it includes your custom post type.For this example, I’ve set
post_type
equal toarray('post, 'page', 'custom-post-type')
… you’ll need to modify that to fit your specific use case.Here’s the widget’s full code for reference:
As of at least 3.6, you can use the following code to modify the query used:
Just add the types you want in the array for post_type and they should appear.
Update: According to http://core.trac.wordpress.org/ticket/16159, this has been available since 3.4
I just came across a great plugin where the heavy lifting is already done, and it has great documentation and author support. I’ve really been impressed.
It allows WP_Query overrides (allowing you to filter by custom post types and anything else you would want) and some clear instructions on how to use it.
Documentation
http://www.pjgalbraith.com/2011/08/recent-posts-plus/
WordPress Plugin URL
http://wordpress.org/extend/plugins/recent-posts-plus/
Made my work just that much shorter!
You can copy the widget code (see /wp-includes/default-widgets.php) and modify the query line.
I have also create a widget plugin for this that’s more customizable than the Recent Posts widget. If interested you can download it here http://new2wp.com/pro/latest-custom-post-type-posts-sidebar-widget/
This Code Creates a New Recent Posts Widget Which Includes Your CPT’s
There’s 2 steps involved when extending the native recent posts widget:
i. Create a new class for your custom recent posts widget which you can do by copying and renaming the recent posts widget code from the defaults-widgets.php in wp-includes folder.
ii. Then you will need to register the new widget as well and you may choose to de-register the native recent posts widget or use both.
All the code can simply be copied into your functions file using a child theme or create another file and include it in your child themes functions file.
Register the new custom recent posts widget
The code includes a modified WP_Query which includes an array for post types including the portfolio CPT which you can rename to match your custom post type.
Here’s the line of code that needs to be modified:
It’s 2020 and I came here to find a solution to the “10 most recent custom post type XYZ”. I found the plugin that does that, and more.
Custom Post Type Widgets extends the usual widget standard post functionalities (most recent, monthly archives, used categories, recent comments, search, calendar) to custom post types.
You select the widget you need (in my case “most recent”) and you get first a select where you specify the custom post type the widget must target. Default choice is good old ‘post’, so this plugin is a substitute of the vanilla WP post-related widgets.