33

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

1

8 Answers 8

25

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:

4
  • 2
    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. Commented May 13, 2011 at 3:07
  • 1
    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
    Commented May 13, 2011 at 3:51
  • 3
    Just an update i used the thinkvitamin link and that helped tremendously and it was a cake walk on setting up custom fields Commented May 13, 2011 at 23:31
  • 2
    How is this an acceptable answer? Links? Really?
    – Bangkokian
    Commented Nov 24, 2020 at 19:37
37

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( 
  'title', 
  'editor', 
  'excerpt', 
  'thumbnail', 
  'custom-fields', 
  'revisions' 
)

Source: https://developer.wordpress.org/reference/functions/register_post_type/#supports

4
  • 5
    Can you please explain why this could solve the issue?
    – s_ha_dum
    Commented Apr 23, 2013 at 14:57
  • 2
    Yes, this works. Who -1'ed the answer. Can you please take it back? Regards, Commented Jul 25, 2013 at 8:59
  • 11
    ...and then.........?
    – Mark
    Commented Oct 26, 2016 at 18:33
  • 7
    This won't solve the issue because OP is asking how to add specifically formatted custom fields ("a drop down and a single line text area"), not enable WordPress's generic "custom fields" feature.
    – tog22
    Commented Oct 4, 2020 at 3:18
19

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

Basically you need two steps 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 steps are globally described here: http://wordpress.org/support/topic/is-it-possible-to-add-an-extra-field-to-a-custom-post-type

Example:

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 or 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 callback 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; ?>">
<?php
}

In the second step you have your custom field to the database. On saving the save_post_{custom_post_type} hook is triggered (since v 3.7, see: https://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' );   
3
  • "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
    Commented Feb 18, 2015 at 5:15
  • "Add a Custom Field called 'function" to a Custom Post Type called 'prefix-teammembers'." What does "called" mean? The name? The singular_name? The label? Maybe it's the string used as the first argument in the register_post_type function. Or maybe it doesn't matter what it is so long as it's consistent.
    – arnoldbird
    Commented Oct 7, 2019 at 17:29
  • Using the prefix_teammembers_save_post save hook appears to make it impossible to insert new custom post fields programmatically with wp_update_post and the meta_input key.
    – kloddant
    Commented Jan 6, 2022 at 17:47
3

The link to thinkvitamin.com in the accepted answer is no longer live. That website has switched it's domain to treehouse.com. I am only answering here because I don't have enough reputation to comment. The content of that expired link is available here: https://blog.teamtreehouse.com/create-your-first-wordpress-custom-post-type for anyone who needs it. Goodluck!

1
  • 1
    Thanks! I was going nuts because I couldn't see the link they were talking about in the comments. Commented Jan 28 at 7:42
2

There are various plugins for custom meta boxes and custom fields. If you look at a plugin that focuses on developers, then you should try Meta Box. It's lightweight and very powerful.

If you're looking for a tutorial on how to write code for a meta box / custom fields, then this is a good start. It's the first part of a series that might help you refine the code to make it easy to extend.

1

I know this question is old but for more info about the topic

WordPress has built-in support for custom fields. If you have a custom post type then all you need is to include 'custom-fields' inside the support array inside of register_post_type as answered by @kubante

Note that this option is also available for native post types like posts and pages you just need to turn it on

Now This custom field is very basic and accepts a string as a value. In many cases that's fine but for more complex fields, I advise that you use the 'Advanced Custom Fields' plugin

0

If you think that you have done everything correctly and you are using Wordpress 5.7 probably all you need is to show it in the additional panel.

click 3 vertical dot on the top left > preferences > Panels

enter image description here

of course this is after you make sure the post type has correct configuration.

-5
// slider_metaboxes_html , function for create HTML 
function slider_metaboxes( ) {
   global $wp_meta_boxes;
   add_meta_box('postfunctiondiv', __('Custom link'), 'slider_metaboxes_html', 'slider', 'normal', 'high');
}

//add_meta_boxes_slider => add_meta_boxes_{custom post type}
add_action( 'add_meta_boxes_slider', 'slider_metaboxes' );

Perfect knowledge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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