Creating Custom Post-types in WordPress

1 comment
If you are thinking of converting your WordPress blog into a CMS(Content Management System), then there is nothing wrong with it. In fact WordPress has been widely used as a CMS. This is due to the fact that WordPress is so much flexible that we can do nearly any type of modification in its functionality. This time we will create custom post-types in WordPress. WordPress already provides 'posts' and 'pages' as post types, but if they don't suit your needs, you can create your own post types. So let's see how it can be done. We will create a new plugin 'Add Persons' for enabling custom post-types.



Initializing our plugin:
<?php
/*
Plugin Name: Add Persons
Plugin URI: http://webspeaks.in/
Description: Add new Persons.
Author: Arvind Bhardwaj
Version: 1.0
Author URI: http://webspeaks.in/
*/
?>
We have just created our new plugin with name 'Add Persons'. Plugin creation has been descibed in the previous post.

Creating New Post-Type:
WordPress provides a very powerful function for creating custom post-types that will take all headaches for us and make us free from writing any clumsy code. The function is 'register_post_type'
The usage goes as:
<?php register_post_type( $post_type, $args ) ?> 
The function 'register_post_type' takes two parameters $post_type and $args. Post tpye is the type of post that we are creating. In this example we will create post of type 'person'.
$args is an array of arguments. Complete reference of the arguments can be found at Function Reference/register post type

Lets write the code for it:
<?php
add_action( 'init', 'create_Person' );/*Called when WrdPress initializes*/

function create_Person() {
 register_post_type( 'person',
  array(
   'labels' => array(
     'name' => __( 'Persons' ), /*general name for the post type, usually plural*/
     'singular_name' => __( 'Persons' ), /*name for one object of this post type*/
     'add_new' => __( 'Add Person' ), /*the add new text*/
     'add_new_item' => __( 'Add New Person' ), /*the add new item text*/
     'edit' => __( 'Edit' ), /*the edit text*/
     'edit_item' => __( 'Edit Person' ), /*the edit text*/
     'new_item' => __( 'New Person' ), /*the new item text*/
     'view' => __( 'View Person' ), /*the view text*/
     'view_item' => __( 'View Person' ), /*the view item text*/
     'search_items' => __( 'Search Persons' ), /*the search items text*/
     'not_found' => __( 'No Persons found' ), /*the not found text*/
     'not_found_in_trash' => __( 'No Persons found in Trash' ), /*the not found in trash text*/
     'parent' => __( 'Parent Person' ), /*the parent text*/
    ), 
   'description' => 'Add new Person to your site!' /*A short descriptive summary of what the post type is*/
   'public' => true, /*Meta argument used to define default values for publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search*/
   'show_ui' => true, /*Whether to generate a default UI for managing this post type*/
   'publicly_queryable' => true, /*Whether post_type queries can be performed from the front end*/
   'exclude_from_search' => false, /*Whether to exclude posts with this post type from search results*/
   'menu_position' => 5, /*below posts*/
   'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ), /*the meta boxes to be shown on the add post page*/
   'taxonomies' => array('post_tag','category'), /*taxonomies to be used*/
  )
 );
}
?>
The parameters are self descriptive in themselves. Comments have been added to clarify the usage.

Accessing our custom post-type:
<?php $loop = new WP_Query( array( 'post_type' => 'person', 'posts_per_page' => 10 ) ); ?>



<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>



 <?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>



 <div class="entry-content">

  <?php the_content(); ?>

 </div>

<?php endwhile; ?>
Passing a 'post_type' parameter in WP_Query can make your life simpler. Now you can access you new post-types just like ordinary posts.
So thats all for our custom post-types. We will meet again for custom meta boxes!

1 comments:

Thanks for adding the comments to the array of labels - that's the sort of thing that seems obvious but can be confusing. Cleared up a couple of things for me!

We would love to hear from you...

back to top