3. What is Post Type?
WordPress can hold and display many different types of content. A
single item of such a content is generally called a post, post is also
a specific post type.
6. Where Post Type Locates?
• Post type stored in “wp_posts” database table
7. When Custom Post Type Needs?
• If you need to Develop websites like:
• www.youtube.com
• www.bbc.co.uk
Etc
• Most Used Custom Post Type and Meta
Fields
• Geo Theme (http://www.geotheme.com)
8. When Custom Post Type Needs?
When Websites Contains:
• Homepage Slider
• Callout Boxes
• Portfolio
• Gallery (Image, Video etc)
• Team/People/Staff
• Job Posting
• Products
• Pricing Table
• etc
12. What we need to create a Portfolio?
• Title
• Custom Field (Website Address)
• Content
• Thumbnail Image
• Taxonomy Category/Tags
13. Register Custom Post Type
register_post_type is called when you need to create or modify
a post_type. register_post_type should only be invoked
through the ‘init’ action.
Reference
http://codex.wordpress.org/Function_Reference/register_post_type
Reserved Post Types
• post
• page
• attactment
• revision
• nav_menu_item
<?php register_post_type( $post_type, $args ) ?>
19. Meta Box field for Custom Post Type
Reference:
http://codex.wordpress.org/Function_Reference/add_meta_box
<?php add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); ?>
add_action('admin_init','sp_portfolio_meta');
function sp_portfolio_meta() {
// add a meta box for WordPress 'project' type
add_meta_box('sp_portfolio', 'Portfolio URL', 'sp_portfolio_meta_setup', 'portfolio', 'side', 'low');
// add a callback function to save any data a user enters in
add_action('save_post','sp_portfolio_meta_save');
}
function sp_portfolio_meta_setup() {
global $post;
?>
<div class="portfolio_meta_control">
<p>
<input type="text" name="_url" value="<?php echo get_post_meta($post->ID,'_url',TRUE); ?>" style="width: 100%;" />
</p>
</div>
<?php
// create for validation
echo '<input type="hidden" name="sp_meta_nonce" value="' . wp_create_nonce(__FILE__) . '" />';
}
20. function sp_portfolio_meta_save($post_id) {
// check nonce
if (!isset($_POST['sp_meta_nonce']) || !wp_verify_nonce($_POST['sp_meta_nonce'], __FILE__))
{ return $post_id;
}
// check capabilities
if (portfolio' == $_POST[post_type']) {
if (!current_user_can('edit_post', $post_id)) {
return $post_id; } } elseif (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
// exit on autosave
if (defined('DOING_AUTOSAVE') == DOING_AUTOSAVE) {
return $post_id;
}
if(isset($_POST['_url'])) {
update_post_meta($post_id, '_url', $_POST['_url']); } else {
delete_post_meta($post_id, '_url');
}
About Nonces:
http://codex.wordpress.org/Function_Reference/wp_create_nonce
25. How to show Contents?
There are few options to retrieve custom posts
query_posts()
WP_Query()
get_posts()
Refefences:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
29. Which one is best?
query_posts() : alter of main query. Post-related
global variables and template tags will be altered.
WP_Query() : defined in wp-includes/query.php.
wp-blog-header.php gives $wp_query object
information. $is_* holds the informations.
get_posts() : returns array of posts. Direct access
to database using WP_Query()
get_posts() is best to use.