Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add one custom meta box to a specific custom meta type "chifres" and two custom meta boxes to a specific custom meta type "presse". This is how far I've gotten:

Here is my code:

//Add custom metadata
function add_custom_metadata_boxes() {
        'custom_meta_box', // $id
        'Values', // $title
        'show_custom_boxes', // $callback
        'foo1', //Custom post types
        'normal', // $context
        'high' // $priority

$prefix = 'custom_';
$custom_meta_fields = array(
            'name' => 'number',
            'label' => 'Number',
            'id' => $prefix . 'number',
            'type' => 'number',
            'std' => ''

// The Callback for the first one
function show_custom_boxes() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

    // Begin the field table and loop
    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field['id'], true);
        // begin a table row with
        echo '<tr>
               <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                switch($field['type']) {
                    // checkbox
                                        case 'number':
                                echo '<input type="number" min="0" step="1" pattern="\d+" placeholder="Fill in a number" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                } //end switch
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table

add_action('add_meta_boxes', 'add_custom_metadata_boxes');

function save_custom_meta($post_id) {
    global $custom_meta_fields;

    // verify nonce
    if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
        return $post_id;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;
    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id))
            return $post_id;
        } elseif (!current_user_can('edit_post', $post_id)) {
            return $post_id;

    // loop through fields and save the data
    foreach ($custom_meta_fields as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
    } // end foreach
add_action('save_post', 'save_custom_meta');

The "chifres" box works perfectly, I just can't figure out how I can add other custom meta boxes to another custom post type.

Any suggestions?

share|improve this question
As already mentioned by @Joe, advanced custom fields is a great way around dealing with the current (slightly) convoluted metabox system. github.com/WebDevStudios/… is a great option as well. –  sagalbot Apr 24 '14 at 20:55

2 Answers 2

This might not be a solution you're looking for, but if you're looking for custom field/box functionality in the back-end of Wordpress, I'd look into Advanced Custom Fields. It's a fantastic plugin that makes modifying the admin side of Wordpress a breeze.

You can add multiple Custom Field sets, put the sets inside of Meta Boxes, and specify exactly which items in the admin control panel that the fields should appear. It's got a great deal of built-in functionality, and there's a group of addons for it for other functionality like creation site Options page, repeater elements, and more.

Best part is that the base plug in is free, and easy to use.


share|improve this answer

The add_meta_box() functions 4th parameter is where you add your CPT's for the backend.

(string) (required) The type of Write screen on which to show the edit screen section ('post', 'page', 'dashboard', 'link', 'attachment' or 'custom_post_type' where custom_post_type is the custom post type slug)

You should be able to add an array for multiple CPT's using the 4th parameter.

For the front end you would use the conditional tag in a custom function or template file depending on how you want to output.



share|improve this answer

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.