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.
'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 '';
echo '
';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '
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.