Home » Custom Post Type with Custom Meta Boxes Part 1 of 2

Custom Post Type with Custom Meta Boxes Part 1 of 2

First lets start with registering a custom post type, to be used at our discretion.There are 5 basic post_types that comes with wordpress these are not to be messed with. They are :

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)

If you use woocommerce you’ll see that basically they also use post_types. For now lets go with something easy.
So the best(simplest) example that i could think of is with a custom photo gallery: You set your featured image there and add text there and it displays on the front end. We’ll cover that part in part 2 lets focus on the basics first

Note: register_post_type should only be invoked through the ‘init’ action. It won’t work at all if called before ‘init’, and aspects of the new post type will work incorrectly if called later.

you should have a look at the codex best place to go i swear it

add_action( 'init', 'reg_photo_init' );
function reg_photo_init() {
$labels = array(
'name' => _x( 'photos', 'post type general name', 'digthis' ),
'singular_name' => _x( 'photo', 'post type singular name', 'digthis' ),
'menu_name' => _x( 'photos', 'admin menu', 'digthis' ),
'name_admin_bar' => _x( 'photo', 'add new on admin bar', 'digthis' ),
'add_new' => _x( 'Add New', 'photo', 'digthis' ),
'add_new_item' => __( 'Add New photo', 'digthis' ),
'new_item' => __( 'New photo', 'digthis' ),
'edit_item' => __( 'Edit photo', 'digthis' ),
'view_item' => __( 'View photo', 'digthis' ),
'all_items' => __( 'All photos', 'digthis' ),
'search_items' => __( 'Search photos', 'digthis' ),
'parent_item_colon' => __( 'Parent photos:', 'digthis' ),
'not_found' => __( 'No photos found.', 'digthis' ),
'not_found_in_trash' => __( 'No photos found in Trash.', 'digthis' ),
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'photo' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
register_post_type( 'photo', $args );

It’s a lot to remember i know. A suggestion then don’t remember it. This is my snippet of code i used over and over again to register post types. Go through it once in the codex understand what the values do once. I won’t go in detail over everything if you have questions i’ll answer them.

Also i have a habit of keeping my code for custom post_types and taxonomies in a different file rather than jumbling them up in my functions.php this isn’t a compulsory thing just a habit. I like keeping my code clean as possible. So i’d load my scripts in something like inc/load_scripts.php and my theme settings in something like inc/theme_sets.php and require them into my code later. For now put this code in a file called inc/custom_posts.php and require them in your functions.php file require('inc/custom_posts.php');

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.