SlideShare a Scribd company logo
WP 201: Custom Post Types,
Custom Fields
WordCamp Columbus 2015 #wccbus | #wp201cp
 Before we describe what a custom post type is, let’s review what is
a post type?
 In WordPress there are 5 post types.
 Post – Blog Entry
 Page – Static Content that doesn’t expire or change.
 Attachment – Document, Image, Video, etc.
 Revision – Version of a Post or Page
 Navigation Menu Item
 A Custom Post Type, is one that you create!
What are Custom Post Types?
 Content that needs to be structured in a certain way, and you wish
to provide a template to ensure the content is always represented
the same.
 Examples:
 Recipes
 Products (non-ecommerce)
 Frequently Asked Questions
 etc.
Why create a Custom Post Type?
How Do I Create a Custom Post
Type?
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}
Sample: https://codex.wordpress.org/Post_Types
 Where do I put the code?
 You could put the code in your themesfunctions.php file.
How Do I Create a Custom Post
Type?
Example Scenario
 Let’s say you have a food blog and you want to use a custom
post type to display your recipes.
 So you use the awesome code we just looked at, customized
it to represent your menu offerings. Created categories for
breakfast, lunch, dinner, appetizers, desserts.
 You have populated your menu with all of your fantastic
recipes.
 Life is good!
Example Scenario
 Now let’s say 3 months or 6 months, or maybe even a whole
year goes by, and everything is still good.
 Then you find the ultimate theme, it’s great, it looks just like
you want your website to look.
 So you install it, then you activate it.
 Then later that day or the next someone emails and asks
where are your recipes, so you tell them to click on recipes,
and they ask WHERE? I’m on your site and there are no
recipes.
Example Scenario
 So you go to your site and check. Hmmm no menu….
 Then in 3…2…1.. You panic!
 Where do I put the code?
 You could put the code in your themesfunctions.php file.
 A better place would be in a plugin!
 A plugin won’t cause your content to disappear.
 How do I create a plugin?
How Do I Create a Custom Post
Type?
Code Demo
What is a Custom Field?
 The Codex defines a custom field as meta-data, that contains
a key/value pair.
 ??????
What is a Custom Field?
 Even I admit it’s pretty technical and confusing.
 If you keep reading the codex page though you’ll see what a
key/value pair is.
 Key – the name of the meta-data
 Value – the meta-data
 Example:
 Key: Currently Reading
 Value: Calvin and Hobbes
What can I do with a Custom
Field?
 You need a feature for your website, such as
announcements.
 First you create a custom post type to handle the
announcement content.
 Title
 Description
 Photo
 It would be cool if I could make announcements appear
automatically on a certain date and disappear automatically
on a certain date.
What can I do with a Custom
Field?
 Create a custom field
 Start Date – built in scheduling of post
 Display End Date
 Importance
 Using some PHP logic when displaying the content we can
automatically display based upon the server’s date the
content, as well as stop displaying it.
 Using similar PHP logic, we can change the CSS class or ID
of the containing element to make the announcement stand
out.
Code Demo
Links Mentioned
 www.generatewp.com – various form based generators for
code snippets in WordPress
 WP Beginner – Site Specific plugin ���
http://www.wpbeginner.com/beginners-guide/what-why-and-
how-tos-of-creating-a-site-specific-wordpress-plugin/
 Recipe Custom Post Type Example –
https://gist.github.com/joecue/d0a5e37f3d1bbe17ae1f
 Announcement Custom Field (Meta Box) and Custom Post
Type Example –
https://gist.github.com/joecue/7d2339e4e9a8b16ab0a4

More Related Content

WP 201 Custom Post Types - Custom Fields - WordCamp Columbus 2015

  • 1. WP 201: Custom Post Types, Custom Fields WordCamp Columbus 2015 #wccbus | #wp201cp
  • 2.  Before we describe what a custom post type is, let’s review what is a post type?  In WordPress there are 5 post types.  Post – Blog Entry  Page – Static Content that doesn’t expire or change.  Attachment – Document, Image, Video, etc.  Revision – Version of a Post or Page  Navigation Menu Item  A Custom Post Type, is one that you create! What are Custom Post Types?
  • 3.  Content that needs to be structured in a certain way, and you wish to provide a template to ensure the content is always represented the same.  Examples:  Recipes  Products (non-ecommerce)  Frequently Asked Questions  etc. Why create a Custom Post Type?
  • 4. How Do I Create a Custom Post Type? add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'acme_product', array( 'labels' => array( 'name' => __( 'Products' ), 'singular_name' => __( 'Product' ) ), 'public' => true, 'has_archive' => true, ) ); } Sample: https://codex.wordpress.org/Post_Types
  • 5.  Where do I put the code?  You could put the code in your themesfunctions.php file. How Do I Create a Custom Post Type?
  • 6. Example Scenario  Let’s say you have a food blog and you want to use a custom post type to display your recipes.  So you use the awesome code we just looked at, customized it to represent your menu offerings. Created categories for breakfast, lunch, dinner, appetizers, desserts.  You have populated your menu with all of your fantastic recipes.  Life is good!
  • 7. Example Scenario  Now let’s say 3 months or 6 months, or maybe even a whole year goes by, and everything is still good.  Then you find the ultimate theme, it’s great, it looks just like you want your website to look.  So you install it, then you activate it.  Then later that day or the next someone emails and asks where are your recipes, so you tell them to click on recipes, and they ask WHERE? I’m on your site and there are no recipes.
  • 8. Example Scenario  So you go to your site and check. Hmmm no menu….  Then in 3…2…1.. You panic!
  • 9.  Where do I put the code?  You could put the code in your themesfunctions.php file.  A better place would be in a plugin!  A plugin won’t cause your content to disappear.  How do I create a plugin? How Do I Create a Custom Post Type?
  • 11. What is a Custom Field?  The Codex defines a custom field as meta-data, that contains a key/value pair.  ??????
  • 12. What is a Custom Field?  Even I admit it’s pretty technical and confusing.  If you keep reading the codex page though you’ll see what a key/value pair is.  Key – the name of the meta-data  Value – the meta-data  Example:  Key: Currently Reading  Value: Calvin and Hobbes
  • 13. What can I do with a Custom Field?  You need a feature for your website, such as announcements.  First you create a custom post type to handle the announcement content.  Title  Description  Photo  It would be cool if I could make announcements appear automatically on a certain date and disappear automatically on a certain date.
  • 14. What can I do with a Custom Field?  Create a custom field  Start Date – built in scheduling of post  Display End Date  Importance  Using some PHP logic when displaying the content we can automatically display based upon the server’s date the content, as well as stop displaying it.  Using similar PHP logic, we can change the CSS class or ID of the containing element to make the announcement stand out.
  • 16. Links Mentioned  www.generatewp.com – various form based generators for code snippets in WordPress  WP Beginner – Site Specific plugin – http://www.wpbeginner.com/beginners-guide/what-why-and- how-tos-of-creating-a-site-specific-wordpress-plugin/  Recipe Custom Post Type Example – https://gist.github.com/joecue/d0a5e37f3d1bbe17ae1f  Announcement Custom Field (Meta Box) and Custom Post Type Example – https://gist.github.com/joecue/7d2339e4e9a8b16ab0a4