Skip to Navigation

Reply to comment

Printer-friendly version

WordPress 2.9: Custom Post Types Explained

WordPress 2.9 is ramping up support for custom post types. It's an improvement, but not nearly as exciting as you've probably been led to believe.

For starters, what is a custom post type in WordPress?

WordPress ships with 4 post types: post, page, revision, and attachment. WP uses a single table, called wp_posts, to store the bulk of its information. Within this table, there is a column called post_type. This column tells WordPress whether an item is a blog post, page, attachment, revision, etc. The idea behind "custom post types" is to add new post_type values (e.g. "event" or "publication"), and handle items differently depending on their post_type.

Custom Fields 101

If any data needs to get saved that doesn't belong in the wp_posts table, it can be saved using custom fields. Each custom field value is saved as a separate row in the wp_postmeta table.

Without the help of plugins (e.g. Custom Field Template), there is no way to auto-assign custom fields to posts depending on their post type. Custom fields are extremely inefficient. Each item is stored as a key/value pair, with the value a LONGTEXT column regardless of what that value actually is (bool, date, integer, etc). If you assign 10 custom fields to a certain post type, then add 5,000 posts, then that's a whopping 50,000 rows cluttering up your wp_postmeta table.

 

What's new in WP 2.9?

get_post_type()
Returns the current item's post type (used within The Loop). This function is identical to $post->post_type.

get_post_types()
Returns an array of all post types.

register_post_type($post_type, $args_array)
Called from within a plugin or functions.php. Only 1 available argument: "exclude_from_search" Ex: $args_array = array('exclude_from_search' => true);

 

What to expect in the future

Currently, custom post types are all but useless without the help of plugins. For example, there is no code within WP core to auto-assign different fields for different post types. The ability to auto-create custom fields depending on the post type would make a lot of sense. There is talk of a UI being added for WP 3.0 to manage custom post types, but only time will tell.

 

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Mollom CAPTCHA (play audio CAPTCHA)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.