There’s at least 3 ways to include one or more custom posts types in the recent posts widget native to WordPress.
- You can use the widget_posts_args filter and simply return the new CPT parameter.
- You can code your very own custom recent posts widget into your theme which looks impressive to clients of quality web designers and developers.
- Or you can find a plugin which includes this function and install it.
1. Custom Function Using widget_posts_args Filter
This PHP code once added to your functions file, enables you to include all single custom post type pages in the native recent posts widget included in WordPress by default.
add_filter('widget_posts_args', 'add_cpt_recent_posts_widget');
function add_cpt_recent_posts_widget($params) {
$params['post_type'] = array('portfolio');
return $params;
}
Simply replace portfolio in the code with the name of your custom post type and separate multiple CPT’s with a comma.
2. Create Custom Recent Posts Widget for CPT’s
This code can be added to your child themes functions file or a new file which you can then include in your functions file using the correct path.
The code includes the portfolio custom post type so simply change the name in the code to your own CPT.
3. Plugin Includes CPT Support
There’s many plugins which enable you to include custom post types in a widget that you can find in the WordPress plugins directory however you could simply create your own plugin on Github using the code above.
Leave a Reply
You must be logged in to post a comment.