WordPress Custom Post Types and 404 Error Fix

Why use custom post types over regular posts? For starters, it easer to find specific post under particular types on the left admin menu than to drill down under posts then categories. This workflow is ideal for when there are multiple contributors reducing the amount of information needed to communicate changes or add content. Think about handing this off to your client who might not as computer literate as you or your web friends.

Here’s a custom post type by the name “Tech Products”. Add the following code to your “functions.php” file under your theme’s directory:

add_action( 'init', 'create_post_type' );
function create_post_type() {
 register_post_type( 'techproduct',
 array(
 'labels' => array(
 'name' => __( 'Tech Products' ),
 'singular_name' => __( 'Tech Product' )
 ),
 'public' => true,
 'has_archive' => true,
 'capability_type' => 'post',
 'supports' => array('title','editor','author','custom-fields','thumbnail'))
 );
}

Above, you don’t have to define ‘supports’. By default, ‘title’ and ‘editor’ (eg: content) are made available when adding or editing a post type. I felt that only having those two as options made too rigid and sparse.

Once this new code has been posted to your server, you should see the new link on your left admin menu maybe under the “comments” link:

Now let’s add a few more bells and whistles to the mix by defining our post type columns.

add_filter("manage_edit-techproduct_columns", "my_techproduct_columns");
function my_techproduct_columns($columns)
{
 $columns = array("title" => "Product Title", 
"description" => "Description", 
"company" => "Company");
 return $columns;
}
add_action("manage_posts_custom_column", "show_techproduct_column");
function show_techproduct_column($column){
 global $post;
if ("ID" == $column) echo $post->ID;
 elseif ("description" == $column) echo $post->post_content;
 elseif ("company" == $column) echo "Adobe";
}

It should show up like so…

PROBLEM WITH LINKING TO CUSTOM POST TYPES!!!!!

You may run into a problem trying to view your custom post type on your live site using Permalinks receiving a 404 error message. I found a fix by simply navigating over to the Permalinks Settings page. There’s no need to Update or Save changes. I believe by viewing this page, it somehow updates it’s self learning of your new custom post type.

If that does not fix your 404 error message, try removed the %category% from my permalink structure or check if your post type conflicts with a pre-existing category name.

If this does not remedy your issue, there is a long thread over on this post:

http://wordpress.org/support/topic/permalinks-404-with-custom-post-type

Leave a Reply