How to show custom Post Types in Recent Posts and Navigation links

If you are somewhat into WordPress development, then you may definitely have come across Custom Post Types. Custom post types are what gives so much power to already powerful WordPress. They bring customization to a whole new level. A medium to big website definitely has one or multiple custom post types, especially those that require different types of layout for different articles. In this post, I’ll show you how to add custom post types in Recent Posts and Navigation Links.

When I started with custom post types, I was overwhelmed especially when it came to formatting and displaying custom post types. I couldn’t find much information in a step-by-step form. And then I needed to display those custom post types in the Recent Posts too and the Navigation links at the bottom of the post too. Again, I came across a plethora of articles on the Web. Most of them included complicated code or changing the core WordPress code. Well, that was scary. You never touch or modify the holy grail!

There should have been some solution that was not this dramatic. And well, there was. I cam e across a simple code but it didn’t work. It just needed a little tweaking and guess what, I was left with the most simple and a 2-line code on how to add custom post types in Recent Posts and Navigation links.

So, let’s get started.

How WordPress treats Posts and Custom Post Types separately

Before we dive into the code, let’s take a look at why we need to add custom post types to some widgets manually. Let’s take a simple example. Suppose there is a website It contains 5 posts. 2 are regular posts and 3 posts are from custom post type ‘Media’. I’ve listed the title, post type, and the date when the post was published below.

  • Star Wars – The Force Awakens; Post Type – Media; Published – 2019-01-02 12:00 AM
  • Are you a Die Hard Star Wars Fan?; Post Type – Post;
    Published – 2019-01-03 12:00 AM
  • Winter has come; Post Type – Post;
    Published – 2019-01-04 12:00 AM
  • Game of Thrones Season – 1; Post Type – Media;
    Published – 2019-01-05 12:00 AM
  • Suits Season – 8; Post Type – Media;
    Published – 2019-01-06 12:00 AM

Now, at any time, if you check the Recent Posts Column in your sidebar, you’ll find:

  • Are you a Die Hard Star Wars Fan?
  • Winter has come

The posts from the custom post type ‘Media’ are not listed here.

Similarly, if you are reading the post ‘Game of Thrones Season – 1’, at the bottom of the post you’ll find ‘Star Wars – The Force Awakens’ as the previous post and ‘Suits Season – 8’ as the next post. If you’re reading ‘Winter has come’, ‘Are you a Die Hard Star Wars Fan?’ will be the previous post while there will be no link in the Next post.

In short, WordPress treats your regular post and post from a custom post type separately, and you need to get your hands a little bit dirty to get a seamless experience between various post types.

How to show Custom Post Types in Recent Posts

To include the posts from custom post types in Recent Posts, you have to define a function that returns an array of custom post types. And then you have to add a filter in Posts widget.

//add this code to your functions.php file
//show custom post types in recent posts
//add_filter('widget_posts_args', 'custom_function_name'); 
//function custom_function_name($custom_parameters) {
//   $custom_parameters['post_type'] = //array('post','custom_post_type_name');
 //  return $custom_parameters;

//Sample Code
add_filter('widget_posts_args', 'add_custom_post_type_to_recent_posts'); 
function add_custom_post_type_to_recent_posts($params) {
   $params['post_type'] = array('post','wordpress_tutorials');
   return $params;

Surprised? Yes, these two lines will make sure that all the posts of the given custom post type (in the above example, wordpress_tutorials) are displayed in the Recent Posts widget in the sidebar.

How to show Custom Post Types in Navigation Links

Navigation Links are the links at the bottom of the post and they take you to the next or previous article from the current one. Here you have to add action to include custom post type when links for previous and next articles are fetched.

//add this code to your functions.php file
//Show custom post types in post navigation links

add_action( 'get_previous_post_where', 'add_custom_post_type');
add_action( 'get_next_post_where', 'add_custom_post_type');
//Third argument is priority(1 to PHP_INT_MAX). The default is 10.
//Fourth argument is the number of argument the function accepts. 
//The default is 1.
function add_custom_post_type( $where ){
//$where is a string that contains the condition based on which the
//next or previous article is fetched.
// $where looks like WHERE p.post_date < '2019-01-30 09:07:03' AND
//p.post_type = 'post' AND ( p.post_status = 'publish' OR p.post_status
//= 'private' )
return str_replace(array( "p.post_type = 'post'", "p.post_type = 'wordpress_tutorials'" ), "(p.post_type = 'post' OR p.post_type = 'wordpress_tutorials')", $where);

Confusing is it? No worries. I’ll explain each step in simpler words. The first two lines are add_action functions. The first function gets the previous post to display in the post navigation based on a condition. The second function similarly gets the next post for the post navigation.

The second argument of both the functions is the same. It is a string that contains the condition based on which a post is selected. In the sample, it says that select a post whose post_date is before the current date, whose post_type is ‘post’, and whose post_status is ‘publish’ or ‘private’.

This is the default condition to get the previous post. Similarly, to get the next post the condition is ‘p.post_date> ‘2019-01-30 09:07:03’ ‘ instead of ‘p.post_date < ‘2019-01-30 09:07:03’ ‘. That date should be greater than the current date.

What we have to do is change the post_type in the condition ($where). We simply use string_replace function to do that. We create an array of post_types. In our case, it’s ‘post’ and ‘wordpress_tutorials’. Now, what the condition does is if it finds ‘p.post_type = ‘post’ in the $where, it replaces it with ‘p.post_type = ‘post’ OR p.post_type = ‘wordpress_tutorials’ ‘. Similarly, if it finds ‘p.post_type = ‘wordpress_tutorials’ in the $where, it replaces it with ‘p.post_type = ‘post’ OR p.post_type = ‘wordpress_tutorials’ ‘.

In short, whether your post is a regular post or of a custom post type, the previous article/ post will always be the one which was last published before the current article/ post. And the same goes for the next article/ post. And if you want to include multiple custom post types, you simply have to add the names of the custom post types separated by comma.

So, that’s all for now. Keep playing with Custom Post Types for extreme customization, but don’t overdo it. And if you get stuck, we are happy to help. (:

Site Footer

© 2018 The Engineer's Cafe. All Rights Reserved.