How to Add Custom Fields to a Custom Post Type?

admin2025-01-07  5

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

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 edited Jul 30, 2012 at 8:52 brasofilo 22.1k8 gold badges69 silver badges264 bronze badges asked May 13, 2011 at 1:59 xLRDxREVENGExxLRDxREVENGEx 1,1991 gold badge15 silver badges24 bronze badges 1
  • Use wordpress.org/extend/plugins/types – Ajay Patel Commented Jul 30, 2012 at 11:21
Add a comment  | 

9 Answers 9

Reset to default 25

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

  • http://wpgear.org/#meta-fields

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

  • http://net.tutsplus.com/tutorials/wordpress/creating-custom-fields-for-attachments-in-wordpress/
  • https://blog.teamtreehouse.com/create-your-first-wordpress-custom-post-type

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

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' );   

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!

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.

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

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

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

function custom_field(){  // custom feils for custom post type stackoverflow
     add_meta_box(
         'custom_field',
         'custom field',
         'custom_field_type',
         'user',
         'normal',
         'low'

     );
 }
 add_action('add_meta_boxes','custom_field');
 
 function custom_field_type($post){
     $state = get_post_meta($post->ID,'state',true);
     $country = get_post_meta($post->ID,'country',true);
     $pincode = get_post_meta($post->ID,'pincode',true);
     $college = get_post_meta($post->ID,'college',true);
     ?>
     <label for = "state"> State : </label>
     <input type ="text" name="state" value="<?php echo $state ?>"><br><br>
     <label for = "country"> Country : </label>
     <input type ="text" name="country" value="<?php echo $country ?>"><br><br>
     <label for = "pincode"> Pincode : </label>
     <input type ="text" name="pincode" value="<?php echo $pincode?>"><br><br>
     <label for = "college"> College Name : </label>
     <input type ="text" name="college" value="<?php echo $college ?>"><br><br>
     <?php
 }

 function handle_custom_field($post_id){
     if(isset($_POST['state'])){
         $state = $_POST['state'];
         update_post_meta($post_id, 'state', $state);
     }
     if(isset($_POST['country'])){
         $country = $_POST['country'];
         update_post_meta($post_id,'country', $country);
     }
     if(isset($_POST['pincode'])){
         $pincode = $_POST['pincode'];
         update_post_meta($post_id,'pincode',$pincode);
     }
     if(isset($_POST['college'])){
        $college = $_POST['college'];
        update_post_meta($post_id,'college',$college);
    }
 }
 add_action('save_post','handle_custom_field');
// 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260468a667.html

最新回复(0)