In WordPress everything is a post_type and anything you would like to attach with this post_type is possible by metaboxes. Posts are stored in the wp_post table and its meta information is stored in wp_postmeta table.

This is a very basic structure which WordPress follows and every new collection is build around it.

posts and pages are the default post_types that come with the WP install.

Let we simplify this by an example.

Requirements: We need to build an collection of events and event will include a title, description, event_date, venue and an thumbnail Image.

By default we can call post-title,description,and thumbnail property of a post type.

Lets create a post type events.

Supports is used to call default options like title, editor,thumbnail and comments.

  • Rewrite will assign a slug for listing page for events and that can be called by a ‘archieve.php’ template.

  • If you want to use a separate template make a copy of archieve.php and rename it ‘events.php’ as it follows the priority {custom-post-type}.php

register_meta_box_cb will decide which function is to be call for meta boxes.

Lets set an action for events metaboxes

Add meta box:

Now following function will add meta box.

Above code is adding two metaboxes while we are following one metabox that will be used to store two meta values

Generation HTML for Meta boxes:

Saving meta data:

Now you have successfully created a metabox for a custom post type which is storing two values ‘location’ and ‘eventdate’. Try by saving this inside of event custom post_type.

You can call a metabox information by post id:

 

Leave a Reply