Recent Posts Element – How to pull through any post type

I saw someone post on a FB group of an explanation on how to pull through custom post types using X Theme.

I wanted to do the same with WooCommerce products a while back and had the code that did that.

function add_product_post_type( $types ) {
  $types['product'] = 'product';
  return $types;
}
add_filter( 'cs_recent_posts_post_types', 'add_product_post_type' );

Code is pretty simple, so lets go through the steps on how to pull through additional custom post types.

Add in a custom post type

If you want to do this manually then I recommend using the site generatewp.com. Its a fantastic resource.

If you want to do it the plugin way, I normally go for the Custom Post Type UI plugin. 

Once activated, use the CPT UI left menu item to add a new post type.

You only need to add in the top 3 fields.

  • Post Type Slug – This would be new_post_type_slug for example. No spaces, no special characters
  • Plural – i.e New Post Type Items
  • Singular – i.e New Post Type Item

Once filled in, copy the slug down as we will need that later, then hit SAVE.

The updated function code

So, now we can add this to the function so that we get that option in the Recent Posts element drop down.

function add_product_post_type( $types ) {
  $types['product'] = 'product';
  // Add in the additional post type here
  $types['new_post_type_slug'] = 'new_post_type_slug';
  return $types;
}
add_filter( 'cs_recent_posts_post_types', 'add_product_post_type' );

Edit  new_post_type_slug to what ever your slug was, then copy this whole code snippet into the function.php file.

If you haven’t, install a child theme. That way you’ll avoid having your custom code overwritten when the theme is updated.

Now when you add in a Recent Posts Element, you’ll be able to select your new custom post type from the drop-down.

Hope this helps and thanks for reading!

Any questions feel free to get in touch!

Leave a Reply

Your email address will not be published. Required fields are marked *