How to implement custom post types and custom fields in WordPress

How to implement custom post types and custom fields in WordPress

Ben Stevinson's Layout avatar

WordPress isn’t just limited to blogging anymore — it’s a fully featured content management system capable of displaying and formatting any kind of content you give it. So let’s talk about two great ways to extend WordPress: custom post types and custom fields.

Custom Post Types

Looks like our test site, Monday Morning Mug (which we used in our email integration blog post), has a new problem to deal with. Our blog has gotten so large that we’ve hired a full-time coffee reviewing staff. They’ve been busy brewing coffee and writing up reviews. They’ve been posting these reviews to the blog, but our readers told us that all of these reviews are getting in the way of our “real” blog content. We need to make a separate place for our coffee reviews, but normal WordPress pages just aren’t going to cut it. The reviewers have also been suggesting that they’d love to have a place to easily store information, such as the country the coffee is from and tasting notes, on the website. This sounds like a perfect use for a custom post type, a WordPress feature that allows us to create new content sections on our site.

Custom post types are incredibly flexible and can be implemented for any kind of content: quotes, videos, and different shades of paint are all examples of what a custom post type could be.

We’re going to want to add a new section of our website in the /coffees/ subdirectory that is full of coffee reviews. This is going to require a bit of code wrangling, but it should be a relatively easy process.

Note: To do this, you’ll need to be able to edit the .php files in your WordPress install directory, which usually requires FTP/SFTP/SSH access. Make sure you have this before you proceed.

Every WordPress theme can utilize a functions.php file, which is located at:
/wp-content/themes/your_current_theme/functions.php
This file is generally used for theme-specific site changes as well as smaller custom site modifications, like the one we’re about to do.

Note: We will be adding code to the functions.php file, which will mean it is theme-specific. If you add this to your functions.php file and then change the theme on your WordPress installation, you will remove the custom post type from your website. You can maintain custom content types by utilizing a plugin, such as Custom Post Type UI.

1. Open up your functions.php file and add the following code at the bottom of the file:

function create_new_post_type() {
$labels = array(
'name' 			 => __('Coffee Reviews'),
'singular_name'  => __('Coffee Review'),
);
$args = array(
'labels'      	 => $labels,
'public' 	  	 => true,
'has_archive' 	 => true,
'menu_position'  => 5,
'description'    => 'Reviews And Types of Coffee',
'rewrite'     	 =>
array('slug' => 'reviews'),
'supports'    	 =>
array( 'title',
'comments', 'editor',
'thumbnail', 'custom-fields', 'revisions'),
);

register_post_type('Reviews', $args);
}
add_action('init', 'create_new_post_type');

Because we don’t particularly like utilizing code without an idea of what it does, let’s walk through what this code does.

Note: You may have noticed that $labels and $args are defined outside of the register_post_type() function even though they are just passed as arguments to that function later on. This method aids organization and readability, but you can condense both arrays into a single argument. It would just be more difficult to understand.

The $args array contains all of the specifics we need to define our new content type. Here’s what these do:

  • ‘labels’ – An array defining what we will call our content type. There are many more labels available that you can use, and a full list is available in the documentation here.
  • ‘public’ – A boolean that defines whether or not the post is viewable outside of the Admin panel.
  • ‘has_archive’ – If true, this boolean will create an archive of this type of custom posts, just like standard WordPress posts.
  • ‘menu_position’ – This integer changes where your custom post navigation button goes. The number 5 indicates that it will go directly below the posts tab. For a full list of numeric codes, check out the same documentation.
  • ‘description’ – Describes the post type.
  • ‘rewrite’ – This changes the slug to be “reviews,” which will help create a usable permalink structure. (Can also be boolean; check the documentation for more information.)
  • ‘supports’ – An array defining what native WordPress editing/post features are enabled. This one is pretty flexible, and there a lot more options, but we will be needing at least custom-fields and editor for this post.

The register_post_type() function is called next, which creates a new post type called Reviews using the array of arguments we just defined.

Finally, we have to call one last function to complete the process. register_post_type() should always be initialized by the action ‘init’. We can hook our new function to init by calling

add_action(‘init’, ‘create_new_post_type’);

Awesome! Now if we log into our WordPress admin panel, we should see a new tab on the left called “Reviews.”

coffee_reviews

2. Now that we have Reviews set up as a custom post type, we need to update the permalink structure so our readers can easily find our posts. Plus it makes our URLs much easier to read and SEO friendly.

3. Under the Settings -> Permalinks menu bar, you’ll find a bunch of common options for permalink structures. We’re going to change the custom setting to match the field /%category%/%postname%/

This means WordPress will generate URLs to match the category and title of our posts.

Note: This is a site-wide change and will affect your posts in other categories.

custom_permalink

Now let’s take a look at the URL of a post our reviewers wrote for the Sumatra Ketiara coffee:

sumatra_permalink

Fantastic! Check out that URL. We have reviews separated into their own directory and the URL is written in plain English. Now our readers know exactly what they’re going to get when they click on a link.

Custom Fields

Although our reviewers love the new Review section, they did ask us to incorporate something on the back end that allows them to enter common info that they write about every coffee, such as country of origin and tasting notes. This sounds like the perfect use of custom fields, which, luckily, are very easy to implement.

1. On an individual review editor, you will see the screen options button at the top right corner of the editor:

screen_options

which expands to this when clicked:

screen_options_dropdown

2. Check the Custom Fields box.

Below the editor, you’ll see the Custom Fields box that we just enabled.

custom_fields_blank

3. You can type in anything in both boxes; anything you type and add in the Name field will be saved for later use. Here, we list the area and country that our coffee is from.

custom_fields_region

Custom fields make it incredibly easy to standardize data that matters for a category across every post in a separate place. Now we just have to get that information to display on our review.

4. We’re going to add this code tag to one of our theme documents:

<br /><?php the_meta(); ?><br />

Note: In this example, we’re going to put this tag in our content.php file, but you will need to find a suitable location for your table of values to go. You should also style this tag to match the rest of your theme, which you can read more about here.

Checking back to our blog, we can see that our tag has inserted a list into our review.

custom_field_example

Swanky! Now our readers and our reviewers are both happy. We were able to build a custom post type that allowed us to save reviews about coffee, and we were able to give our reviewers a place to store all of their important information for every different review.

Comments ( 110 )

  1. Richardmat

    May 7, 2025

    vavada вход [url=http://vavadavhod.tech/#]vavada вход[/url] вавада

  2. ZackaryCaush

    May 7, 2025

    http://vavadavhod.tech/# vavada

  3. Richardmat

    May 6, 2025

    pin up вход [url=http://pinuprus.pro/#]пин ап казино официальный сайт[/url] пин ап зеркало

  4. ZackaryCaush

    May 6, 2025

    https://pinupaz.top/# pinup az

  5. ZackaryCaush

    May 5, 2025

    https://vavadavhod.tech/# vavada вход

  6. Richardmat

    May 5, 2025

    pin up azerbaycan [url=https://pinupaz.top/#]pin up casino[/url] pin up

  7. ZackaryCaush

    May 5, 2025

    http://pinupaz.top/# pin-up casino giris

  8. Richardmat

    May 5, 2025

    пин ап вход [url=http://pinuprus.pro/#]пин ап казино[/url] пин ап казино официальный сайт

  9. ZackaryCaush

    May 5, 2025

    http://pinuprus.pro/# pin up вход

  10. ZackaryCaush

    May 4, 2025

    https://vavadavhod.tech/# вавада казино

  11. Richardmat

    May 4, 2025

    pin up вход [url=http://pinuprus.pro/#]пин ап вход[/url] pin up вход

  12. ZackaryCaush

    May 4, 2025

    http://pinupaz.top/# pin-up

  13. ZackaryCaush

    May 2, 2025

    https://pinuprus.pro/# пин ап вход

  14. ZackaryCaush

    May 1, 2025

    https://pinuprus.pro/# пин ап казино официальный сайт

  15. ZackaryCaush

    May 1, 2025

    https://pinupaz.top/# pin-up

  16. Richardmat

    May 1, 2025

    пин ап вход [url=http://pinuprus.pro/#]пин ап казино[/url] пин ап казино

  17. Richardmat

    May 1, 2025

    vavada вход [url=https://vavadavhod.tech/#]вавада официальный сайт[/url] vavada

  18. ZackaryCaush

    April 30, 2025

    http://pinuprus.pro/# pin up вход

  19. Richardmat

    April 30, 2025

    пин ап вход [url=http://pinuprus.pro/#]пин ап зеркало[/url] пин ап казино официальный сайт

  20. Kennethsheby

    April 30, 2025

    пин ап казино официальный сайт: пин ап казино - пин ап казино официальный сайт

  21. ZackaryCaush

    April 30, 2025

    http://pinupaz.top/# pin up azerbaycan

  22. ElmerSip

    April 30, 2025

    пинап казино: pin up вход - pin up вход

  23. Richardmat

    April 30, 2025

    pin-up [url=http://pinupaz.top/#]pin up azerbaycan[/url] pin up az

  24. Kennethsheby

    April 30, 2025

    пин ап вход: пин ап казино - пин ап зеркало

  25. ZackaryCaush

    April 30, 2025

    https://pinupaz.top/# pin up casino

  26. ElmerSip

    April 30, 2025

    пин ап казино: pin up вход - pin up вход

  27. Richardmat

    April 30, 2025

    пинап казино [url=https://pinuprus.pro/#]пин ап вход[/url] пин ап зеркало

  28. Kennethsheby

    April 30, 2025

    пинап казино: пин ап вход - пин ап вход

  29. ZackaryCaush

    April 30, 2025

    https://vavadavhod.tech/# вавада казино

  30. ElmerSip

    April 30, 2025

    pinup az: pinup az - pin-up

  31. Kennethsheby

    April 30, 2025

    пинап казино: pin up вход - пин ап казино

  32. Richardmat

    April 30, 2025

    pin-up [url=https://pinupaz.top/#]pin-up casino giris[/url] pin up

  33. ZackaryCaush

    April 30, 2025

    https://vavadavhod.tech/# vavada

  34. ElmerSip

    April 30, 2025

    pin-up: pin-up casino giris - pin-up casino giris

  35. Stevendrype

    April 30, 2025

    canadian pharmacy scam: canadian pharmacy meds reviews - canadian family pharmacy

  36. Michaeljouch

    April 29, 2025

    canadapharmacyonline [url=https://expressrxcanada.shop/#]Generic drugs from Canada[/url] canadian pharmacy meds reviews

  37. Walterhap

    April 29, 2025

    http://medicinefromindia.com/# Medicine From India

  38. Dannysit

    April 29, 2025

    trusted canadian pharmacy: canadapharmacyonline com - vipps approved canadian online pharmacy

  39. MichaelFaulp

    April 29, 2025

    real canadian pharmacy: legit canadian pharmacy - canadian pharmacy uk delivery

  40. Walterhap

    April 29, 2025

    https://expressrxcanada.shop/# northwest canadian pharmacy

  41. Dannysit

    April 29, 2025

    safe canadian pharmacy: Express Rx Canada - canadian drug pharmacy

  42. Michaeljouch

    April 29, 2025

    canadian pharmacy meds reviews [url=https://expressrxcanada.shop/#]ExpressRxCanada[/url] canadian online pharmacy

  43. Stevendrype

    April 29, 2025

    mexico pharmacy order online: RxExpressMexico - mexico pharmacy order online

  44. MichaelFaulp

    April 29, 2025

    canadian pharmacy 24: Buy medicine from Canada - canadian family pharmacy

  45. Walterhap

    April 29, 2025

    http://expressrxcanada.com/# canadian pharmacy ltd

  46. Dannysit

    April 29, 2025

    Rx Express Mexico: Rx Express Mexico - mexico drug stores pharmacies

  47. Stevendrype

    April 29, 2025

    Rx Express Mexico: mexico pharmacies prescription drugs - RxExpressMexico

  48. Michaeljouch

    April 29, 2025

    Rx Express Mexico [url=https://rxexpressmexico.shop/#]mexico drug stores pharmacies[/url] mexico drug stores pharmacies

  49. Walterhap

    April 29, 2025

    https://rxexpressmexico.com/# mexican online pharmacy

  50. Dannysit

    April 29, 2025

    online canadian pharmacy: Express Rx Canada - canadian pharmacies

  51. MichaelFaulp

    April 29, 2025

    reputable canadian online pharmacies: Express Rx Canada - reliable canadian pharmacy

  52. Michaeljouch

    April 29, 2025

    indian pharmacy [url=https://medicinefromindia.shop/#]reputable indian pharmacies[/url] Medicine From India

  53. Walterhap

    April 29, 2025

    https://rxexpressmexico.com/# pharmacies in mexico that ship to usa

  54. Stevendrype

    April 29, 2025

    Rx Express Mexico: mexican online pharmacy - RxExpressMexico

  55. Dannysit

    April 29, 2025

    legal to buy prescription drugs from canada: drugs from canada - canadian pharmacy price checker

  56. MichaelFaulp

    April 29, 2025

    canada ed drugs: Canadian pharmacy shipping to USA - canadian pharmacies online

  57. Michaeljouch

    April 28, 2025

    canadian drugstore online [url=http://expressrxcanada.com/#]ExpressRxCanada[/url] best canadian pharmacy to buy from

  58. Stevendrype

    April 28, 2025

    mexico pharmacy order online: mexican online pharmacy - Rx Express Mexico

  59. Walterhap

    April 28, 2025

    https://expressrxcanada.shop/# safe online pharmacies in canada

  60. Dannysit

    April 28, 2025

    indian pharmacy: Medicine From India - medicine courier from India to USA

  61. Michaeljouch

    April 28, 2025

    canadian drug pharmacy [url=https://expressrxcanada.com/#]Express Rx Canada[/url] canada drugstore pharmacy rx

  62. Stevendrype

    April 28, 2025

    indian pharmacy online: medicine courier from India to USA - Medicine From India

  63. Walterhap

    April 28, 2025

    https://expressrxcanada.shop/# maple leaf pharmacy in canada

  64. Dannysit

    April 28, 2025

    medicine courier from India to USA: MedicineFromIndia - indian pharmacy

  65. MichaelFaulp

    April 28, 2025

    medicine courier from India to USA: MedicineFromIndia - Medicine From India

  66. Stevendrype

    April 28, 2025

    indianpharmacy com: mail order pharmacy india - indian pharmacy online shopping

  67. Walterhap

    April 28, 2025

    https://medicinefromindia.com/# MedicineFromIndia

  68. Dannysit

    April 28, 2025

    mexico pharmacy order online: Rx Express Mexico - mexican rx online

  69. Michaeljouch

    April 28, 2025

    precription drugs from canada [url=https://expressrxcanada.shop/#]Express Rx Canada[/url] canada drugs online

  70. MichaelFaulp

    April 28, 2025

    reliable canadian online pharmacy: Generic drugs from Canada - recommended canadian pharmacies

  71. Stevendrype

    April 28, 2025

    mexico pharmacy order online: RxExpressMexico - RxExpressMexico

  72. Dannysit

    April 28, 2025

    indian pharmacy online shopping: indian pharmacy - indian pharmacy online

  73. BilliesniCt

    April 28, 2025

    pharmacie en ligne france pas cher: Pharmacie en ligne France - Pharmacie Internationale en ligne pharmafst.com

  74. Bradleyfup

    April 28, 2025

    Kamagra pharmacie en ligne: Achetez vos kamagra medicaments - achat kamagra

  75. BernardVeida

    April 28, 2025

    Cialis sans ordonnance 24h: Cialis generique prix - Tadalafil 20 mg prix sans ordonnance tadalmed.shop

  76. Robertmut

    April 27, 2025

    https://tadalmed.shop/# Achat Cialis en ligne fiable

  77. BernardVeida

    April 27, 2025

    Pharmacie sans ordonnance: pharmacie en ligne - pharmacie en ligne france livraison belgique pharmafst.com

  78. BernardVeida

    April 27, 2025

    Cialis sans ordonnance pas cher: Acheter Cialis 20 mg pas cher - Tadalafil sans ordonnance en ligne tadalmed.shop

  79. Robertmut

    April 26, 2025

    https://pharmafst.shop/# pharmacie en ligne france livraison internationale

  80. BilliesniCt

    April 26, 2025

    Acheter Viagra Cialis sans ordonnance: Acheter Viagra Cialis sans ordonnance - Tadalafil sans ordonnance en ligne tadalmed.shop

  81. PeterUnomb

    April 26, 2025

    pharmacie en ligne fiable [url=http://pharmafst.com/#]Pharmacie en ligne France[/url] Pharmacie sans ordonnance pharmafst.shop

  82. Bradleyfup

    April 26, 2025

    Kamagra pharmacie en ligne: Kamagra Commander maintenant - kamagra pas cher

  83. BernardVeida

    April 26, 2025

    Cialis generique prix: Cialis sans ordonnance pas cher - Acheter Cialis 20 mg pas cher tadalmed.shop

  84. BilliesniCt

    April 26, 2025

    Pharmacie en ligne Cialis sans ordonnance: Acheter Viagra Cialis sans ordonnance - cialis sans ordonnance tadalmed.shop

  85. Robertmut

    April 26, 2025

    https://tadalmed.com/# cialis prix

  86. Bradleyfup

    April 26, 2025

    п»їpharmacie en ligne france: Meilleure pharmacie en ligne - п»їpharmacie en ligne france pharmafst.com

  87. BilliesniCt

    April 26, 2025

    Kamagra pharmacie en ligne: Kamagra Commander maintenant - achat kamagra

  88. Robertmut

    April 26, 2025

    https://pharmafst.com/# pharmacie en ligne france fiable

  89. Robertmut

    April 26, 2025

    https://kamagraprix.shop/# acheter kamagra site fiable

  90. BilliesniCt

    April 26, 2025

    Achetez vos kamagra medicaments: Acheter Kamagra site fiable - kamagra oral jelly

  91. PeterUnomb

    April 25, 2025

    kamagra gel [url=http://kamagraprix.com/#]Acheter Kamagra site fiable[/url] Achetez vos kamagra medicaments

  92. Robertmut

    April 25, 2025

    http://kamagraprix.com/# kamagra 100mg prix

  93. BilliesniCt

    April 25, 2025

    pharmacie en ligne france pas cher: pharmacie en ligne - trouver un mГ©dicament en pharmacie pharmafst.com

  94. Robertmut

    April 25, 2025

    https://pharmafst.com/# Pharmacie en ligne livraison Europe

  95. BernardVeida

    April 25, 2025

    Kamagra Commander maintenant: Achetez vos kamagra medicaments - Kamagra Oral Jelly pas cher

  96. Bradleyfup

    April 25, 2025

    Achetez vos kamagra medicaments: kamagra oral jelly - kamagra gel

  97. PeterUnomb

    April 25, 2025

    trouver un mГ©dicament en pharmacie [url=https://pharmafst.com/#]pharmacie en ligne[/url] pharmacie en ligne france livraison internationale pharmafst.shop

  98. Robertmut

    April 25, 2025

    https://pharmafst.com/# pharmacie en ligne sans ordonnance

  99. BernardVeida

    April 25, 2025

    Pharmacie Internationale en ligne: pharmacie en ligne pas cher - pharmacies en ligne certifiГ©es pharmafst.com

  100. BilliesniCt

    April 25, 2025

    achat kamagra: kamagra en ligne - kamagra pas cher

  101. Bradleyfup

    April 25, 2025

    Acheter Viagra Cialis sans ordonnance: Achat Cialis en ligne fiable - Cialis sans ordonnance pas cher tadalmed.shop

  102. BernardVeida

    April 25, 2025

    trouver un mГ©dicament en pharmacie: Pharmacies en ligne certifiees - pharmacie en ligne fiable pharmafst.com

  103. PeterUnomb

    April 25, 2025

    acheter mГ©dicament en ligne sans ordonnance [url=https://pharmafst.com/#]Meilleure pharmacie en ligne[/url] pharmacie en ligne livraison europe pharmafst.shop

  104. DavidSaisp

    April 24, 2025

    kamagra en ligne: achat kamagra - Kamagra Commander maintenant

  105. Edwardtoupe

    April 23, 2025

    olympe casino cresus: olympe casino avis - olympe casino cresus

  106. Anthonykek

    March 7, 2025

  107. RandyZorie

    February 22, 2025

    These girls aren't just centered on the physical; a number of them are eager to take part in sensual stripping, pleasuring their orgasmic selves, and also indulging in the pleasures of others. All of the actions shown in these galleries will captivate, as these black beauties pour their all into every efficiency. http://nakedafricawomen.com/ extensive collection of images offers ample materials to fulfill all your desires - especially if you've got a penchant for ebony goddesses. As the motion unfolds, witness stiff people erupting with enthusiasm and smiling faces adorned with the aftermath. Featuring a plethora of dark porn stars, hundreds of top-notch images, and a variety of hardcore scenes, this collection of galleries is sure to please even the most discerning of tastes.

  108. Paola

    October 17, 2014

    Thank you so much for this post! It was a life saver for me right now.
    Regards.

  109. inward

    June 19, 2014

    Hello! Do you know if they make any plugins to protect
    against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any suggestions?

    • Ben Stevinson

      June 23, 2014

      Hey there!

      Security should definitely be a concern for every WordPress maintainer, and I definitely understand your worries!

      Two awesome services we'd recommend for WordPress security are VaultPress and iThemes Security Pro. VaultPress is actually a service of Automattic, the company responsible for WordPress, and iThemes Security Pro is developed by some very well respected WordPress leaders. Both are great options.

      Of course, if you're a Flywheel user, we proactively handle security for you and make daily backups, both of which we offer at no extra cost. On the off chance that your site is hacked, we will restore and fix it for free.

  110. Tony

    June 12, 2014

    I don't think a conversation about custom fields in WordPress can be complete without mentioning the Advanced Custom Fields plugin. The functionality of ACF makes developing sites (and sites which are almost apps) a breeze. For simple things, the built-in custom fields are good, but for anything beyond strictly string based key/value data, definitely check out ACF. http://www.advancedcustomfields.com/

Join the discussion