3 Ways To Include Custom Post Types In Native Recent Posts Widget

There’s at least 3 ways to include one or more custom posts types in the recent posts widget native to WordPress.

  1. You can use the widget_posts_args filter and simply return the new CPT parameter.
  2. You can code your very own custom recent posts widget into your theme which looks impressive to clients of quality web designers and developers.
  3. 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.

Widgets - Recent Posts

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.

custom recent posts widget


Comments

2 responses to “3 Ways To Include Custom Post Types In Native Recent Posts Widget”

  1. I would have a similar request. I need to modify the Genes_Featured_Post widget. Indeed it has only “All categories” and “your categories” options in the dropdown menu, while I need to add a “Current category” option which shows “All categories” everywhere but “Current category” if is_category() is true, like this snippet: https://gist.github.com/anonymous/24cada3a79838a7b7267

    It would be very cool I think, but I cannot understand where to modify the widget (there are so many lines), or better solution, how to put the code in the right place using an add_action in my functions.php child theme.

    Thank you in advance if you’ll decide to help me.

    1. Brad Dalton Avatar
      Brad Dalton

      Mario

      The Genesis Featured Posts widget is totally different to the recent posts widget and not native to WordPress.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.