Custom Post Type Best Practices

I have used Custom Post Types in almost every WordPress project I have worked on since the feature was introduced in WordPress 3.0.

When collaborating with other developers you also come across some common mistakes, mistakes that I’ve also made in the past. So I would like to share some things to keep in mind when using Custom Post Types.

The $post_type should always be singular

The codex does not explicitly say it anywhere, but all the Codex examples use singular names for $post_type. This is probably the most common mistake I see developers make. If you want the new post type’s URL to have a plural word in it, you can specify it in the slug argument.

Use a namespace for your post types

This is something that even I haven’t done. But the codex makes a good point in their own best practices section. A conflict can occur between theme and plugin post types if you use a common word as the post type name and it is not prefixed with a namespace. So for instance if your post type is a product, do not simply name is product but rather yournamespace_product.

Remember the template hierarchy when creating specific post type templates

WordPress template hierarchy is great, so please use it! I have seen cases where developers have created a new page with an associated template where they query for the posts instead of simply using the default template file to create an archive page for example.

From the codex:

In the same way that posts are shown on their own page with single.php, custom post types will use single-{posttype}.php if it’s available.

Same thing applies to archive-{posttype}.php. This visual overview of the template hierarchy is something I keep referring to.

Adding custom columns to the admin can be easy!

Joost De Valk provides these code snippets that can come in handy when working with custom post types. Often you also have custom taxonomies or other meta data linked to your post types which you might want to present in the admin. Great post!

Advertisements
About

Web Developer @ Oakwood Creative - www.oakwood.se

Tagged with: , , ,
Posted in Best Practices

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 120 other followers

%d bloggers like this: