Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,004 Points

Custom Post Types in Wordpress

Hello great Treehouse people. :)

There's one thing I still find baffling about Wordpress and that's Custom Post Types. I've looked around and watched the videos on them but I'm non the wiser about what they actually are and how to use them.

My understanding is that they're types of pages that aren't covered by the uses for both Posts and Pages. So they're pages on your site that have their own custom designed functionality.

But I can't seem to find how to find, create or use them.

I looked around on this link for the answer which seems to suggest I add them in by code. But on the Wordpress for website owners course Zac seems to have them all ready made.

Did I miss a setting or a step when I set up my Wordpress site?

2 Answers

Kevin Korte
Kevin Korte
28,135 Points

They are confusing to wrap your head around at first, and what I had to do was to realize that the word post is a bit misleading, but it is how they are called in WP.

I assume you understand the difference between a post and a page in WP, and when you should use one or the other. If not, or for anyone else, a page is a static element, and about us page, a contact page, etc...posts are for content that might have a life cycle (new article), or similar to other posts (portfolio project), or most obviously, a blog post.

WP was and still kinda is a blogging platform at it's heart. But custom post types allows you to create a new, reference-able group of content. Let's say might sight had a calendar, portfolio projects, and a blog. Well, I could have 3 post types. My blog could stay with the default posts types, which is referenced in the WP admin menu as Posts, I could have a calendar custom post type, which would could get a calendar link in the admin menu, and I could have a portfolio post type, which would have it's own admin link. The custom post types would inherit the look and feel of a default post type. Usually, at this point, this is when you would assign custom fields to the custom post types. For instance, my calendar custom post type needs to allow me to enter in very different data than my blog post type would.

The nice thing is that all of these custom post types still gets saved in the standard WP Post table, but are reference-able by their post type, using the WP_Query class, passing in an argument to the type of post you want, such as `post_type = calendar

At their core, custom post types can be added manually, by you in your functions.php file. There are examples how to do that in the codex here

I believe Zac uses a very popluar plugin that allows you to create custom post types with a graphic user interface, and than the plugin generates the code for you. The plugin works so well, I use it too. The plugin I am sure he used is the custom post types UI plugin.

Like I said, a custom post type isn't usually much good without custom fields, which is where the [Advanced Custom Fields] plugin comes into play, where you can set up custom fields and content types, and than assign those fields to a custom post type. These two plugins work very nicely together.

how can i do now