WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok, so I have registered a few custom post types and a few taxonomies. Now, for the life of me, I cannot figure out the code I need to add a Custom Field to my Custom Post Type.

I need a drop down and a single line text area. But I also need to have separate fields for post types. So, say post type one has 3 fields and post type 2 has 4 fields but the fields are different.

Any tips would help I have looked at the codex and found something but cannot make sense of what I need to add to my functions.php file

share|improve this question
Use wordpress.org/extend/plugins/types – Ajay Patel Jul 30 '12 at 11:21
up vote 12 down vote accepted

This is probably more complicated than you think, I would look into using a framework:

If you want to write your own , here are some decent tutorials:

share|improve this answer
really it would be that hard. I thought it would be as simple as adding a register code to my functions like we do with post types and taxonomies. – xLRDxREVENGEx May 13 '11 at 3:07
I'll plus one this answer, but it's not too complex. The thinkvitamin.com link does a great job explaining how to add the metaboxes and save them. The sltaylor.co.uk link is an awesome tutorial on using some great coding practices. My word of caution is be careful when using the save_post hook. It's called at weird times. Make sure to have WP_DEBUG variable set to true in order to see potential errors that arise when using it. – tollmanz May 13 '11 at 3:51
Just an update i used the thinkvitamin link and that helped tremendously and it was a cake walk on setting up custom fields – xLRDxREVENGEx May 13 '11 at 23:31

Add/edit the supports argument ( while using register_post_type ) to include the custom-fields to post edit screen of you custom post type:

'supports' => array( 
share|improve this answer
Can you please explain why this could solve the issue? – s_ha_dum Apr 23 '13 at 14:57
Yes, this works. Who -1'ed the answer. Can you please take it back? Regards, – JeyKeu Jul 25 '13 at 8:59

Although you should have to add some validation, this action does not seem to be complicated for the current versions of WordPress.

Basically you need two step to add a Custom Field to a Custom Post Type:

  1. Create a metabox which holds your Custom Field
  2. Save your Custom Field to the database

These step are global describe here: http://wordpress.org/support/topic/is-it-possible-to-add-an-extra-field-to-a-custom-post-type


Add a Custom Field called "function" to a Custom Post Type called "prefix-teammembers".

First add the metabox:

function prefix_teammembers_metaboxes( ) {
   global $wp_meta_boxes;
   add_meta_box('postfunctiondiv', __('Function'), 'prefix_teammembers_metaboxes_html', 'prefix_teammembers', 'normal', 'high');
add_action( 'add_meta_boxes_prefix-teammembers', 'prefix_teammembers_metaboxes' );

If your add of edit a "prefix-teammembers" the add_meta_boxes_{custom_post_type} hook is triggered. See http://codex.wordpress.org/Function_Reference/add_meta_box for the add_meta_box() function. In the above call of add_meta_box() is prefix_teammembers_metaboxes_html a call back to add your form field:

function prefix_teammembers_metaboxes_html()
    global $post;
    $custom = get_post_custom($post->ID);
    $function = isset($custom["function"][0])?$custom["function"][0]:'';
    <label>Function:</label><input name="function" value="<?php echo $function; ?>">

In the second step you ave your custom field to the database. On saving the save_post_{custom_post_type} hook is triggered (since v 3.7, see: http://stackoverflow.com/questions/5151409/wordpress-save-post-action-for-custom-posts). You can hook this to save your custom field:

function prefix_teammembers_save_post()
    if(empty($_POST)) return; //why is prefix_teammembers_save_post triggered by add new? 
    global $post;
    update_post_meta($post->ID, "function", $_POST["function"]);

add_action( 'save_post_prefix-teammembers', 'prefix_teammembers_save_post' );   
share|improve this answer
"why is prefix_teammembers_save_post triggered by add new?" have you found an answer, i am also stumbling on a extra function trigger which i can't recall? – alex Feb 18 '15 at 5:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.