Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Add Custom Meta Boxes in WordPress Posts and Post Types

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Add Custom Meta Boxes in WordPress Posts and Post Types

Just last week, we showed you how to add custom meta fields to custom taxonomies in WordPress. That article brought our attention to another important topic of how to add custom meta boxes in WordPress posts and post types. We wrote about a plugin method of adding meta boxes and custom write panels using More Fields plugin, but the development of that plugin has stopped. We had linked to some good tutorials that showed how to do add custom meta boxes in WordPress from our Custom Fields 101 article however all those are no longer available. Therefore in this article, we will show you how to add custom meta boxes in WordPress posts and custom post types.

Note: This tutorial is for designers and developers.

Because the goal of this article is to show you how to create reusable meta boxes in WordPress, we will be utilizing a Custom Metaboxes and Fields for WordPress class (CMB) by Andrew Norcross (@norcross), Jared Atchison (@jaredatch), and Bill Erickson (@billerickson). All three of these guys are very well reputed in the WordPress community. They combined their efforts to create a powerful solution to make their job and other’s jobs a lot easier.

First thing you need to do is download the CMB class. Create a new folder and call it “custom-meta-boxes-wp”. Extract the CMB zip files in here.

Now let’s create a blank PHP file, and call it cmb-wp.php. This is the file that we will use to define our plugin and create all the meta boxes that we need. Below is an example content that you can place in this file:

Plugin Name: Sitename Custom Meta Boxes
Plugin URI:
Description: Create Meta Boxes for Sitename.
Version: 0.1
Author: Syed Balkhi
Author URI:
License: GPL v2 or higher
License URI: License URI:

//Initialize the metabox class

function wpb_initialize_cmb_meta_boxes() {
	if ( ! class_exists( 'cmb_Meta_Box' ) )
		require_once(plugin_dir_path( __FILE__ ) . 'init.php');

add_action( 'init', 'wpb_initialize_cmb_meta_boxes', 9999 );

//Add Meta Boxes

function wpb_sample_metaboxes( $meta_boxes ) {
	$prefix = '_wpb_'; // Prefix for all fields

	$meta_boxes[] = array(
		'id' => 'test_metabox',
		'title' => 'Test Metabox',
		'pages' => array('page'), // post type
		'context' => 'normal',
		'priority' => 'high',
		'show_names' => true, // Show field names on the left
		'fields' => array(
				'name' => 'Test Text',
				'desc' => 'field description (optional)',
				'id' => $prefix . 'test_text',
				'type' => 'text'

	return $meta_boxes;
add_filter( 'cmb_meta_boxes', 'wpb_sample_metaboxes' );

The above example is very basic. It just adds a sample test meta box with a text field on “pages”. As you can see, that you have the option to customize the post type, and just about everything. CMB class supports the following field types:

  • text
  • text small
  • text medium
  • text money
  • date picker
  • date picker (unix timestamp)
  • date time picker combo (unix timestamp)
  • time picker
  • color picker
  • textarea
  • textarea small
  • textarea code
  • select
  • radio
  • radio inline
  • taxonomy radio
  • taxonomy select
  • checkbox
  • multicheck
  • Image/file upload

Here is the wiki page that will show you how to add all the field types. There is also an example-functions.php file that you can use to reference.

Once you are done, upload the folder in your plugins folder. Now, you can easily display these fields in your loop by using get_post_meta() function. Here is a tutorial if you want to display custom fields outside the loop.

Ever since we found this CMB class, we have not looked back. We use this on all of our projects and client projects as well. If you are a designer/developer, then you will find this to be really easy and help you improve your workflow. We hope that this tutorial has helped you add custom meta boxes in WordPress posts and post types.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Hafiz says:

    i have some custom meta fields in my custom post type. i want to add publish button with these meta fields within meta box. please give me idea to do this.

  2. Lau says:

    it does not work. I place the code in functions.php, and I get this error:

    Warning: require_once(/var/www/…/init.php): failed to open stream: No such file or directory in /var/www/…/wp-content/themes/quinn/functions.php on line 206

    Fatal error: require_once(): Failed opening required ‘/var/www/…/init.php’ (include_path=’.:/usr/share/php:/usr/share/pear’) in /var/www/…/wp-content/themes/quinn/functions.php on line 206

    what is this init file for?

  3. Marios says:


    I had this plugin installed and worked ok. Now i want to remove it but it causes errors to my site.

    I have noticed that if i remove the created cmb-wp.php the errors appear. I believe is because it creates the metabox and is not removed after the de-activation of the plugin.

    How can i safely remove the metabox and de-activate the plugin ?

    PLEASE HELP this is urgent !

  4. Sam says:

    I can’t get mine to output??

  5. Sam says:

    I’m having problems outputting it, I can see the field in the back-end and added some content however I use;


    and nothing appears, I’ve tried numerous methods and no success??

    Am I missing something?

  6. JOHN BOB says:

    HI I love the plugin and everything is working so well on pages but my main conscern is to display them on custom post types and posts is there any way I can do this?

    I tried to change this
    ‘pages’ => array(‘page’), // post type to ‘pages’ => array(‘post’), // post type

    It only made it available on posts but not on custom post types. Please is there any one who can help me here???

    • Junaid Hassan says:

      Try following:

      ‘pages’ => array(‘custom-post-type-name’), // post type to ‘pages’ => array(‘post’), // post type

      e.g if your custom post type is ‘movies’ then use following:

      ‘pages’ => array(‘movies’), // post type to ‘pages’ => array(‘post’), // post type

  7. John Crumpton says:

    Probably better to use Advanced Custom Fields now as the UI is excellent and speeds everything up

  8. Shoelaced says:

    So… just so you all have this knowledge, “cmb” stands for “comme ma bitte” in French, which is basically their version of “That’s what she said…”

    Ex.: “Oh, la la! C’est enorme! (cmb)”

    Aside from my immature giggling it’s working perfectly, but be sure to download the trunk branch.

  9. Mário Vital says:

    the same problem with me !

  10. Vicky Dalmia says:

    Hi can we use to create metabox in category pages?

  11. kachan64 says:

    I have installed as instructed and I can’t get my content to display on the page. Please help…

  12. Rajneesh says:


    Everything is working fine for me only one issue with show custom filed data in front end.

    Warning: Missing argument 1 for get_post_meta()
    defined in wp-includes/post.php on line 1769

    Any suggestion please.


  13. Hash Varsani says:


    I’ve got this installed and working fine so far. Just wondering how to add field types for URL and email if they are not listed in he supported list.

  14. Sergey says:

    Thanks for the post.
    But I can’t understand – where shold all these files (class + cmb_wp) should be located?

  15. Daniele says:


    i followed the tutorial but after uploaded folder, i can’t see anything… where am i wrong?

    • Editorial Staff says:


      If you follow the instructions word for word, then you will have a metabox under pages with a sample text field.

  16. Victoria says:

    I’m having a similar problem as Errol, I lose all data once I click the update or publish button and nothing comes up when I get post meta. I had this happen in a theme before but in that case it be visible on the site but magically migrate to the custom fields section once it was published. I can’t find this data ANYWHERE… Could it be the “_wpb_” prefix being used? I noticed the example functions do not have that.

    I’m no wizard at this stuff but I’ve tried deactivating the plugins, and re-installing THIS plugin with no avail. Any help would be appreciated!!! Thanks.

    • Jeffrey Samorano says:

      To Victoria and Errol, I was having the same problem and had to make sure that the IDs had no spaces. I don’t know if this applies to your individual situation but it worked for me.. My situation was that *some* were saving and some were not. I noticed the ones that were not saving had spaces in the array ID (‘id’ => ‘id-name’)… Hope this helps!

  17. Мах says:

    How to use this class for add the castom metaboxes to the category?

  18. Dorijan says:

    I was wondering if it was possible to specify which metaboxes to display at which page (I have multiple but need only certain metaboxes on certain template pages)?

  19. VegasKev says:

    For some reason when using this script all line breaks are lost when the data is stored in the db tables so if you have a custom post meta that is for a description (like I do), you cannot enter multiple paragraphs, as they line breaks for those paragraphs will not be displayed in your custom post type’s post template, since they are lost during the save process.

    Does anyone have a fix for this? I really need to be able to have multiple paragraphs in a few of my fields and I’m not sure how to modify the code to accept the line breaks entered into the field when it is saved.

    Any guidance would be greatly appreciated. Thanks in advance.

    • Editorial Staff says:

      Use a wysiwyg field and see if the issue is the same. You can have line breaks if you use HTML paragraph tag or line break tags.

      • VegasKev says:

        Thanks for the tip. I didn’t even think of that. I wonder if a normal carriage return will automatically create a line break as it does if you do a carriage return in the WP post editor?

  20. The Cableguy says:

    Man I simply can not get my custom fields to display in my theme at all. This is friggen nutz~!

    • VegasKev says:

      Have you registered your custom post type and created your custom post template yet?

      If not, start with those and then knock out your metabox & custom post meta work, then add your custom post meta ids from your metaboxes for your custom post type to be displayed in your custom post template and you will be all good.

      • The Cableguy says:

        Oh, ok… I simply thought that all I had to do was create the plugin and then simply use something like
        ID, $key, true); ?> and simply change “mykey” to the key value I made and change “key” to the same thing to make my custom fields display in my single.php file, lol. I did not know I had to create other files and all.
        The custom fields do show up in my post editor, oh well, I guess its time to get reading, lol.

      • The Cableguy says:

        Oh, ok… I simply thought that all I had to do was create the plugin and then simply use something like
        ID, $key, true); ? > and simply change “mykey” to the key value I made and change “key” to the same thing to make my custom fields display in my single.php file or my salesposts.php , lol. I did not know I had to create other files and post types and register stuff for it to work, lol. .
        The custom fields do show up in my post editor, just cant get them to display in my posts, oh well, I guess its time to get reading, lol.

        • VegasKev says:

          No problem. If you need help with custom post types and post templates, let me know, they’re fairly easy once you get the hang of them.

  21. Kev says:

    Here’s the pastebin…sorry…was lazy…you can edit my comments to minimize clutter if you want.

    Again, I appreciate any guidance.

    • Editorial Staff says:

      Is this a custom WP query? Try using get_the_id instead of $post->ID. See if that works for you.

  22. Alvin says:

    I would like to use reusable profile fields for my buddypress site.
    Users can show their “interests” within their profile by adding a new textfield , now i want to add as many as they need (max 10)
    Is this possible ? I dont have a clue on where to start , help ?

  23. J A McRae says:

    Many thanks for the info. It works; I would like to learn some more :)

    1) Where is a good place to learn more about Classes. I’d like to understand a little better what it is I’m applying here. Not an easy term to google

    2) What if I want to use this code in my themes as opposed to a plugin? Where could I put this folder, if i put the code portion in my functions file?


  24. Toni Michel says:

    Hi there! I am trying to follow this tutorial to creat a subtitle field in my custom post_type team.

    I think i’m doing the same you are in here but it seems that the field doesn’t show up in the wp-admin when adding a new team post

    • Editorial Staff says:

      Can you send the pages field in an array like we are doing. Just to see if that fixes the problem.

      • Toni Michel says:

        Hi there, thanks for your reply!

        I changed to:

        ‘pages’ => array(‘team’)

        But it gives me the same result. No errors but the field is not added.. any idea (the post_type team works)?

  25. Oliver says:

    Great work. just one question: does it work for custom post types? how would I define it?

    • Editorial Staff says:

      Yes it does. In line 30 of the code, define your custom post type in an array. Like we have page. You can have page, post. You can have page, post, books, movies, recipes….

  26. Errol says:

    I have followed this word for word – as it stands I get just about everything to show up as it should.
    However when I publish the page I seem to loose all the content (data) which I have placed into the field, seems like it is not saving to the db?



    • Editorial Staff says:

      That’s weird. If you are using the class, then it shouldn’t happen. Could be a conflict with another plugin. Can you try deactivating all plugins and see if the class works by itself.

  27. Praveen says:

    Please could you help how to show meta box in posts instead of pages.

    I changed this line ‘pages’ => array(‘page’), // post type
    ‘pages’ => array(‘posts’), // post type
    ‘pages’ => array(‘post’), // post type
    ‘posts’ => array(‘post’), // post type

    but of no use. please help…

    • Editorial Staff says:

      This is how we are using it on WPBeginner, and it works fine:

      'pages'      => array( 'post', ), // Post type
      • Praveen says:

        Thanks a lot. I figured it out. Actually i have been editing local copy of the plugin and expecting it to work. This tutorial is awesome. It helped me a lot.

        My website is a deals website and i am searching for custom fields from long time so that i can add expiry time, mrp and discount price etc. Finally i implemented this on my site.

  28. DZ.Slick says:

    Nice tut here. Just wanted to know if I’m allowed to develop this into a different kind of plugin.

  29. vajrasar says:

    Great Post!
    I used it to make Custom Meta Box as Cast, Genre, Director and Show Timings. I followed the instruction. Downloaded everything to plugins directory (activated the plugin) made the php file, used your code and altered it to make my desired field.
    Even, the boxes successfully showed up below all posts (i changed post type to Posts instead of Pages). But the real problem is how can I show it on front end?
    I used get_post() and also visited everything possible on net in past 3 hours, but no success. If you can point out anything. Thanks.

    • Editorial Staff says:

      You have to use get_post_meta function.

      • Vajrasar says:

        Yea got it working, was using duplicate field names [Bill Erickson helped to point it out]. Thank You for such an awesome piece on Custom MetaBoxes. Thumbs Up.

  30. Mattia Frigeri says:

    Ok, seems interesting… thanks.
    But for us not so expert, you should explain where to put which file in which folder, inside our wordpress installation!

    • Dunners says:

      Totally agree, this all sounds very exciting and just what I’m looking for but the post lacks major detail about implementing this. Also the code of the example-functions.php seems to totally knock out my blog!

      Any chance that this post can be fleshed out?

      • Editorial Staff says:

        If you read the note in the post, it clearly says that this post is for designers and developers. There are certain things that cannot be broken down further without making it insanely long and boring. Which part are you stuck with in the tutorial? Here is a simple step by step:

        1. Download the Zip and Extract it
        2. Upload the folder into your wp-content/plugins/ as a new plugin.
        3. You should see a new plugin showing up in your admin panel. Activate it.
        4. Use the reference wiki to add meta fields as you please.

  31. Jukka Hotokka says:

    Thank you! This is very helpful article.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.