Setting up custom post types in WordPress

Custom post types in WordPress are, amongst other things, suited to providing lists of items which you’d like to conent manage but don’t necessarily require an individual page themselves. The Clients page on creativeglo.co.uk is just such a page. The finished page combines custom post types and custom taxonomies. Set out below is an overview of how the custom post types part was set up.

First up, in to the functions.php file goes code to create and register a new custom post type the function can contain more than one custom post type . add_action() initialises the custom post type.

function create_my_post_types() {
// add post types here
}
//
add_action( 'init', 'create_my_post_types' );

An array for each of the required post types then needs to be added using add register_post_type(). Create a new array inside register_post_type() for each type to be registered. Here I’m registering a type called client_list. There are a number of settings here which will determine where the UI for the post type will be displayed and how it will work. As others have taken the time to explain these in detail, there are links at the bottom to detailed explainations.

function create_my_post_types() {
register_post_type( 'client_list',
array(
'labels' => array(
'name' => __( 'My Clients' ),
'singular_name' => __( 'My Client' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add new client' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit client' ),
'new_item' => __( 'New client' ),
'view' => __( 'View client' ),
'view_item' => __( 'View client' ),
'search_items' => __( 'Search clients' ),
'not_found' => __( 'No clients found' ),
'not_found_in_trash' => __( 'No clients found in Trash' ),
'supports' => array (
'title',
'editor',
'custom-fields'
)
),
'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'menu_position' => 20,
'query_var' => true,
'taxonomies' => array('category','post_tag'),
'capability_type' => 'post',
)
);
}
//
add_action( 'init', 'create_my_post_types' );

Now in the admin panel I can see a new sub-menu added to the admin screen menu. From here it’s possible to add and manage custom post types pretty much just as you can with ordinary posts.

But wait. If all this gets you posts which are pretty much like ordinary posts, what’s the point? Well, my old clients page was made from a list of clients which were regular posts. Various pieces of code were required to prevent client posts appearing in the archives, the RSS, ¬†in search results, ¬†in menus and the site map. By making use of custom post types the situation is reversed. None of these things will happen by default, but it’s possible to make them happen if that’ s what’s required. Likewise, if you’d like each custom post type to have its own page, and comments, and all that other stuff, it’s possible, but it will happen only when you put in place code to make it happen,

Basically, the new post type is ready for items to be added and the next step is to set up the page where the items will be displayed. On the clients page I set up the following query and loop.


<?php	// loop through posts of the custom post type 'client_list'
$loop = new WP_Query( array( 'post_type' => 'client_list', 'posts_per_page' => 10 ) ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
// looping code here
<?php endwhile; // end while have clients ?>

The new WP_Query referred to by $loop will search for all posts of the custom post type  client_list. The next steps are to add some clients and, to create a taxonomy structure to relate projects to clients. Read part three, creating custom taxonomies

Futher reading

Tags: