WordPress – Creating Custom Post Types

Today I am working with custom post types. I want to allow the admin of the site I am creating to easily create various types of posts with different properties. None of the posts will be too complicated – in fact, many of them may just be a title and body text – but I want the admin to easily choose the exact post type from the left-hand nav. I don’t want him or her to have to create a basic “Post” then choose a category. It may seem like a nitpick-y distinction (and probably a less flexible solution) but I think it will make it easier for the admin to create specific types of posts.

I found three plugins that I’m considering using for custom post types:

I didn’t even try Pods. The overview video was sort of tedious and the plugin just didn’t seem like what I was looking for.

I played around a bit with Types and it seemed ok, but the interface to create new types felt a little clunky to me.

Wordpress - Types plugin

I guess it seemed too complicated. That may very well mean it’s more powerful than other plugins, but I’ll have to discover for myself if I need that power.

I decided to go with Custom Content Type Manager. The overview video made the plugin easy to use and indeed, my first experiments were straightforward. I’m probably going to move forward either with CCTM or a custom solution.

One thing that I think is common to all these plugins: When I am creating a custom type and adding custom fields, the fields always appear after the body text editor. Is there any way to reposition fields in WordPress’ new post editor?

I found this post on StackOverflow that describes how hide the body text editor. That seems like a good place to start.

< … trying that solution … >

Hmm .. it removes the body text editor if I set $post_type = “page” but it doesn’t work if I set it to “story”. And when I use this debugging statement:

echo "_wp_post_type_features = " . print_r($_wp_post_type_features, true) . "\n";

I can see that the only post types are “post,” “page,” “attachment,” “revision,” and “nav_menu_item.” Where is “story”? 

I found a couple other forum threads that seem to be looking to do exactly what I’m trying to do, but their solutions aren’t working for me or would only work for plain Posts. They suggest a number of global variables that could be useful, but none of them want to work for my scenario. Here are the variables I am trying to use and their values when I am editing a Story (a custom content type):

  • $post->post_type = empty
  • $post_type = empty
  • $current_screen = empty
  • $wp_post_types = big array of post types and their details; Stories not among them.
  • $_wp_post_type_features = more simple array of post types and some details; Stories not among them.
Lots of questions being raised here. The first three variables don’t display anything when I’m editing a plain Post either. Are these variables deprecated or something?

< … lots of research and tinkering … >

For some reason the CCTM plugin just wasn’t registering my new post type correctly (could very well be user error), so I began researching creating custom post types manually. I’ve found some good resources that explain the process pretty well, so I think I’m going to try to go this route instead.

For starters, I followed this tutorial by Richard Shepherd. The tutorial resulted in a barebones post type – good enough for starters. While going through the tutorial, I sidetracked into a bunch of WordPress function reference pages that are extremely useful and clearly explained:

One thing I can’t figure out and can’t find any information about: If I add several new post types to my functions.php file, it’s going to get quite long and unruly. How can I properly organize my it file in this case?

I have seen an offhanded suggestion that post types should be created within the context of a plugin, but not much more information beyond that. One of the benefits, apparently, of moving the creation of post types to a plugin is that the plugin can be shared across all themes, whereas the functions.php file is specific to the theme in which it is created. That sounds good to me.

Another question I have at this point: How do I create more complex custom fields like images or videos (or fields that can either be an image or a video)?

To hedge my bets – and I can do this because I’m basic in experimentation mode and everything I’m doing can just be thrown away if necessary – I am creating skeleton custom post types and I am also creating corresponding categories that can be used with generic Posts. I have kept my functions.php file somewhat clean by extracting the code for each of the custom types into their own PHP files, then included them in the functions.php file.

It makes a lot of sense to me to get these post types out of the theme, but for now I still don’t see how to do that. Do I create a plugin called client-name that includes the custom post types? Do I create a separate plugin for each post type? I don’t think I need to figure this out now, but eventually I do.

For now I am going to work on learning how to add the appropriate custom fields to each post type. As things stand, each of the four post types I created (Products, Stories, Locations, People) is just like a standard Post: A title and an editor (body text editor). There’s a chance things could be left just like this and still work. The admin would then to add things like images, videos, and secondary headers into the big editor.

Before getting into custom fields, I have run across a strange issue: I cannot get the excerpts from one of my custom posts (a product) to update. I added a second product and it works fine. If I update it’s body text the excerpt updates along with it. But the initial product’s doesn’t.

Update: Deleted the post and re-created it and it works fine. That was a dumb waste of time.

Update 2: Good blog post by Eric Mann about putting custom post types in plugins and keeping them out of themes. His Stacked theme gives a quick example of how he practices what he preaches.

Leave a Reply