Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Different Custom Meta Fields on Multiple Custom Post Types WordPress

  • SOLVED

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="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;
}
?>

Answers (1)

2011-07-13

Joshua Nelson answers:

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.


Jake Caputo comments:

Hey Josh,
Yeah I tried changing the names throughout and that doesn't seem to work. I'm going to check out that walkthrough now.


Joshua Nelson comments:

Try calling out your $meta_box array like this each time:


$meta_boxes[] = ...


Joshua Nelson comments:

sorry, this:


$meta_box[] = ...


Jake Caputo comments:

I can't get that to work either.

Also, I tried that walkthrough and it's throwing these errors, but the file IS there. So I'm not sure what's going on.

Warning: require_once(CHILD_DIR/lib/metabox/init.php) [function.require-once]: failed to open stream: No such file or directory in /homepages/28/d319552840/htdocs/wp-content/themes/faithadvantage/functions.php on line 209

Fatal error: require_once() [function.require]: Failed opening required 'CHILD_DIR/lib/metabox/init.php' (include_path='.') in /homepages/28/d319552840/htdocs/wp-content/themes/faithadvantage/functions.php on line 209


Joshua Nelson comments:

You should place that require_once() after you callout the custom meta boxes per his walkthrough. if you don't call them out like he has it shown, then you'll get an error. If you call it before you declare values for the meta_boxes you'll also get an error. It should all go in the same folder as your functions file, call it out as


require_once('lib/metabox/init.php');


but make sure you've already declared your meta_boxes, similar to this:


$prefix = 'rt_';
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'thinker_details',
'title' => 'Thinker Details',
'pages' => array('thinker'), // post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Thinker Details',
'desc' => 'Information to be displayed about this thinker',
'type' => 'title'
),
array(
'name' => 'Location',
'desc' => 'City, Country (Used for mapping)',
'id' => $prefix . 'location',
'type' => 'text_medium'
),
array(
'name' => 'Sources',
'desc' => 'Sources of material, projects involved in, links. One entry per line',
'id' => $prefix . 'sources',
'type' => 'textarea'
),
array(
'name' => 'Short Bio',
'desc' => 'Shorter biography for display where space is limited. Try to keep below 200 characters or around 50 words',
'id' => $prefix . 'short_bio',
'type' => 'textarea_small'
),
array(
'name' => 'Rank',
'desc' => 'Between 1 and 100, numbers only.',
'id' => $prefix . 'rank',
'type' => 'text_small'
),
array(
'name' => 'Photo Upload',
'id' => $prefix.'image',
'desc' => 'Attach image file (jpg, png, gif)',
'type' => 'file_list',
),
array(
'name' => 'Website',
'desc' => 'Url to Personal or Organization\'s site',
'id' => $prefix . 'website',
'type' => 'text_medium'
),
)
);


Joshua Nelson comments:

After that code, then place your require_once


Jake Caputo comments:

Got it! It's working.

This code didn't work

require_once(CHILD_DIR . '/lib/metabox/init.php');

but it did when I replaced it with

require_once('lib/metabox/init.php');