How to add custom field to custom post type?

32,529

Solution 1

You can use this plugin

http://wordpress.org/extend/plugins/types/

or this tutorial may helpful for you.

http://wptheming.com/2010/08/custom-metabox-for-post-type/

Solution 2

As Muhammad Yasin said there are plugins I'd recommend:
http://wordpress.org/extend/plugins/more-fields/

if you want to do it yourself in code look at: add_meta_box

<?php add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); ?>

You can register boxes per post type.

Solution 3

If you want to create a custom meta box inside a custom post type you will need to use 3 functions.

  1. A function to create a custom "meta boxes" block/screen on your post edit screen: add_meta_boxes_{$post_type}

  2. A function to add a input field to change/show your custom meta

  3. And finally a function to save your meta along with the rest of the post when you click save on the post edit screen: save_post_{$post->post_type}

In your case, a custom checkbox would look like this:

add_action( 'add_meta_boxes_products', 'meta_box_for_products' );
function meta_box_for_products( $post ){
    add_meta_box( 'my_meta_box_custom_id', __( 'Additional info', 'textdomain' ), 'my_custom_meta_box_html_output', 'products', 'normal', 'low' );
}

function my_custom_meta_box_html_output( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'my_custom_meta_box_nonce' ); //used later for security
    echo '<p><input type="checkbox" name="is_this_featured" value="checked" '.get_post_meta($post->ID, 'team_member_title', true).'/><label for="is_this_featured">'.__('Featured Product?', 'textdomain').'</label></p>';
}

add_action( 'save_post_team_member', 'team_member_save_meta_boxes_data', 10, 2 );
function team_member_save_meta_boxes_data( $post_id ){
    // check for nonce to top xss
    if ( !isset( $_POST['my_custom_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['my_custom_meta_box_nonce'], basename( __FILE__ ) ) ){
        return;
    }

    // check for correct user capabilities - stop internal xss from customers
    if ( ! current_user_can( 'edit_post', $post_id ) ){
        return;
    }

    // update fields
    if ( isset( $_REQUEST['is_this_featured'] ) ) {
        update_post_meta( $post_id, 'is_this_featured', sanitize_text_field( $_POST['is_this_featured'] ) );
    }
}
Share:
32,529
Cynthia
Author by

Cynthia

Veteran web developer and SEO consultant with 15+ years' experience.

Updated on April 15, 2020

Comments

  • Cynthia
    Cynthia about 4 years

    Good morning.

    I have created a custom post type called 'Products'. I want to create a custom field (is metabox the correct term?) where my client can tick a box to determine whether a given post within this CPT is a featured post.

    Here is the code in my functions.php to create the 'Products' CPT:

    function products_custom_init() {
    
        $labels = array(
            'name' => _x('Products', 'post type general name'),
            'singular_name' => _x('Product', 'post type singular name'),
            'add_new' => _x('Add New', 'products'),
            'add_new_item' => __('Add New Product'),
            'edit_item' => __('Edit Product'),
            'new_item' => __('New Product'),
            'view_item' => __('View Product'),
            'search_items' => __('Search Products'),
            'not_found' =>  __('Nothing found'),
            'not_found_in_trash' => __('Nothing found in Trash'),
            'parent_item_colon' => ''
        );
    
        $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_nav_menus' => false,
            'query_var' => true,
            'rewrite' => array('slug','pages'),
            'capability_type' => 'post',
            'hierarchical' => true,
            'menu_position' => 5,
            'supports' => array('title','editor','thumbnail','excerpt',)
          );
    
        register_post_type( 'products' , $args );
    }
    add_action( 'init', 'products_custom_init' );
    

    So how do I add the 'featured' metabox / custom field to only Products posts?

    Many thanks,

    Cynthia