Horje
Different Custom Meta Fields on Multiple Custom Post Types - Wordpress Solution
I have 3 custom post types and I need different custom meta fields on each one. The code I have works for one, but not multiple (I have multiple instances of $meta_box that cancel each other out). All the changing of the code I've tried doesn't work. Here's my code, but if you have something else that works I'm cool with that too.

<?php
$prefix = 'faith_ad_';

$meta_box = array(
    'id' => 'faith_ad-meta-box1',
    'title' => 'Vendor Info',
    'page' => 'vendors',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Link',
            'desc' => 'Enter the full URL to this vendor\'s website including the http://.',
            'id' => $prefix . 'vendor_link',
            'type' => 'text'
        ),
		array(
            'name' => 'PDF',
            'desc' => 'Enter the full URL to the PDF for this vendor. You may upload it using the Media uploader on the top left.',
            'id' => $prefix . 'pdf_link',
            'type' => 'text'
        )
    )
);

$meta_box = array(
    'id' => 'faith_ad-meta-box2',
    'title' => 'Slide Info',
    'page' => 'slides',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Learn More Link',
            'desc' => 'Enter the link to where you would like this slide to link to, if anywhere.',
            'id' => $prefix . 'slide_link',
            'type' => 'text'
        ),
    )
);

$meta_box = array(
    'id' => 'faith_ad-meta-box3',
    'title' => 'Testimonial Info',
    'page' => 'testimonials',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Organization',
            'desc' => 'Enter the name of the organiztion this person is part of. ',
            'id' => $prefix . 'testimonial_organization',
            'type' => 'text'
        ),
		array(
            'name' => 'Location',
            'id' => $prefix . 'testimonial_location',
            'type' => 'text'
        )
    )
);

add_action('admin_menu', 'faith_ad_add_box');

// Add meta box
function faith_ad_add_box() {
    global $meta_box;
    
    add_meta_box($meta_box['id'], $meta_box['title'], 'faith_ad_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}

// Callback function to show fields in meta box
function faith_ad_show_box() {
    global $meta_box, $post;
    
    // Use nonce for verification
    echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    
    echo '<TABLE class="w3-table w3-striped w3-bordered w3-border w3-white" class="form-table">';

    foreach ($meta_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);
        
        echo '<tr>',
                '<th style="width:140px;"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>';
        switch ($field['type']) {
            case 'text':
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                break;
        }
        echo     '<td>',
            '</tr>';
    }
    
    echo '</table>';
}


add_action('save_post', 'faith_ad_save_data');

// Save data from meta box
function faith_ad_save_data($post_id) {
    global $meta_box;
    
    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_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;
    }
    
    foreach ($meta_box['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
		
		//echo '<Br />new: '.$new;
		//echo '<Br />old: '.$old;
        
        if (strlen($new) > 0 && $new != $old) {
			//echo '<Br />update'.$field['id'].$new;
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
	//exit;
}
?>


Solution - 1

You should be calling out

'custom_post_type'
instead of
'page'
I believe... Also, you should use a different string name like:

$meta_box_one = ...
$meta_box_two = ...
$meta_box_three = ...
Then call these out specifically in your faith_ad_add_box function. Or, add this line:

add_action('admin_menu', 'faith_ad_add_box');
After each instance where you define $meta_box Also, check out Bill Erickson's walkthrough on this, he's got some code you can download and place in your theme folder. I use this to create custom meta boxes of all different types on multiple custom post types without problem: http://www.billerickson.net/wordpress-metaboxes/ It's a lot less work to display them as well, all that's included in his code library.





Wordpress

Related
Add loading animation to plugin - Wordpress Solution Add loading animation to plugin - Wordpress Solution
Excerpt Issue - linkage - Wordpress Solution Excerpt Issue - linkage - Wordpress Solution
HTTP Referrer - Contact Form 7 - Transients API - Wordpress Solution HTTP Referrer - Contact Form 7 - Transients API - Wordpress Solution
How to Hide Buddypress Admin Bar For Everyone Except Admin - Wordpress Solution How to Hide Buddypress Admin Bar For Everyone Except Admin - Wordpress Solution
Add Custom Sidebar to wpfolio - Wordpress Solution Add Custom Sidebar to wpfolio - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7