Custom Post Type Snippets to make you smile

Joost de Valk is the founder and CEO of Yoast. He's a WordPress / Web developer, SEO and an Open Source fanatic.

So it’s friday, I’ve been coding all day and I thought I’d share some of the cool snippets I’ve come across and/or developed today. I’ve mostly been working with Custom Post Types and Taxonomies, so let me share some of that goodness. Let’s geek out in a bit, but first let me show you why this is cool, be sure to click the image, so you can see which functionality I’ve added to the otherwise boring custom posts overview screen:

Click for large version

Add columns to the overview page for a Custom Post Type

So you’ll want to add some columns to your post type’s overview page, or remove some. Don’t forget to replace <CPT> with your own custom post type in all these examples:

Filter Custom Posts by Custom Taxonomy

Ok so far this is all fairly simple. Now let’s go a bit more advanced. Let’s say you have a custom taxonomy attached to that custom post type and you want to show a filter for that custom taxonomy on the custom post types overview page, just like you have a categories drop down on the posts overview page. This code was taken (though slightly modified) from this thread.

20 Responses to Custom Post Type Snippets to make you smile

teebee • 8 years ago

One thing that is confusing me is where these snippets go. Are they all in the functions.php file or in multiple files? If in multiple files maybe you can update your post to tell which file each snippet goes into. Thanks!

Danny G Smith • 8 years ago

This is a wonderful tutorial, I am using it on a site that I am working on now. One thing that is not intuitive to me is why you can only fire the functions to “Filter Custom Posts by Custom Taxonomy” once, even if you create more than one custom post type. If they run more than once, none of them work. Oh, well, great stuff.

Dan • 8 years ago

Where is the Order field in Custom Post Types? Is this something that has to be added when registering the CPT?

Dan • 8 years ago

Just answered my own question. Added the Page Attributes to the Supports argument when I Register Post Types.

Marcus • 8 years ago

nice find, great snippets, bookmarked for future reference 🙂

Bryan Carter • 8 years ago

Excellent information, thanks for the snippet/tutorial!

Sander • 8 years ago

Thanks Joost, this is stuff I can actually use on some projects I’m working on right now.. Thanks for sharing!

Good stuff here, I’d done some custom column stuff for my CPTs before, but this is icing on the cake so to speak!

Lars Koudal • 8 years ago

I agree with the others, great work as always.

You actually just given me a great idea, thanks 🙂

Nicusor Dumbrava • 8 years ago

If you intend to build a plugin …. let us know 🙂

Craig • 8 years ago

Man, awesome info you put out on your site, I put “Quix” in my tool bar two days ago, mind boggling what you can do with that tool. Installed Robots Meta on 3 sites we run, 1st site saw a 1000% increase in long tail keywords in the first month, second site saw a 200% increase, 3rd site 100% increase. Consider me a huge fan of your work 🙂 Commenting so I can subscribe to your newsletter.

This is a tutorial with great snippets. Joost just knows that most of us just end up copy and pasting anyway.

Micah • 8 years ago

This is essential for plugin developers to learn. I have implemented columns and filters on my Promotion Slider plugin, but never really got around to making the columns sortable. Thanks again for a great post!

Nicusor Dumbrava • 8 years ago

If you use any Joost’s code do not forget to give him some credits 🙂
Micah Wood, just kidding 🙂

Micah • 8 years ago

Ha ha 🙂 You do have to be careful though, Joost’s code is so well written, that if you look to see how he did something, you can’t help but be tempted just to copy it! But hey, I have no problem giving credit where credit is due…

Nicusor Dumbrava • 8 years ago

Yes Micah, especially since these credits add value to your plugin. I am not a programmer, but I peek the code of plugins that I use and I like it when I see mentions with credits given to reputable developers … gives me confidence in the product overall 😉