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

Trying to use 2 different custom meta boxes on 2 custom post type WordPress

  • SOLVED

I have used this tutorial to create a custom meta box to use on a custom post type:

http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/

I need to have 2 custom meta boxes with different fields for 2 different post types. So I duplicated the code to make an extra custom meta box, but the problem is the same meta box is showing for both custom post types instead of each having it's own.

I can provide FTP for someone to fix this.

Here is the code from functions.php


// Add the Meta Box
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box', // $id
'Custom Meta Box', // $title
'show_custom_meta_box', // $callback
'event', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');

// Field Array
$prefix = 'custom_';
$custom_meta_fields = array(
array(
'label'=> 'Artist Name',
'desc' => 'The Artist Name',
'id' => $prefix.'artistname',
'type' => 'text'
),
array(
'label'=> 'Date and Location',
'desc' => 'Month XXXX City',
'id' => $prefix.'datelocation',
'type' => 'text'

)
);

// The Callback
function show_custom_meta_box() {
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>
<td>';
switch($field['type']) {
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}

// Save the Data
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');






// Add the Meta Box
function add_custom_meta_box2() {
add_meta_box(
'custom_meta_box', // $id
'Custom Meta Box', // $title
'show_custom_meta_box', // $callback
'painting', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box2');

// Field Array
$prefix = 'custom_';
$custom_meta_fields = array(
array(
'label'=> 'Type',
'desc' => 'eg. Tempera on Canvas',
'id' => $prefix.'type',
'type' => 'text'
),
array(
'label'=> 'Size',
'desc' => 'eg. 96\' x 60\' 2010',
'id' => $prefix.'size',
'type' => 'text'

),
array(
'label'=> 'City',
'desc' => 'eg. New York',
'id' => $prefix.'city',
'type' => 'text'

)

);

// The Callback
function show_custom_meta_box2() {
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>
<td>';
switch($field['type']) {
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}

// Save the Data
function save_custom_meta2($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_meta2');



Answers (3)

2012-07-05

Kailey Lampert answers:

In the second add_meta_box() call, you need to change the callback

from:
'show_custom_meta_box', // $callback

to:
'show_custom_meta_box2', // $callback


Dan | gteh comments:

I tried that, still doesn't work.


// Add the Meta Box
function add_custom_meta_box2() {
add_meta_box(
'custom_meta_box', // $id
'Custom Meta Box', // $title
'show_custom_meta_box2', // $callback
'painting', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box2');



This meta box is still showing for both post types


Dan | gteh comments:

I figured it out.

Your change, plus I had to change $custom_meta_fields to $custom_meta_fields2

thanks

2012-07-05

Francisco Javier Carazo Gil answers:

As Kailey says this is the problem:

function add_custom_meta_box2() {
add_meta_box(
'custom_meta_box', // $id
'Custom Meta Box', // $title
'show_custom_meta_box2', // $callback
'painting', // $page
'normal', // $context
'high'); // $priority
}

add_action('add_meta_boxes', 'add_custom_meta_box2');


Try to use better names in your functions to prevent this kind of problems.

2012-07-05

Luis Abarca answers:

Try changing the array name $custom_meta_fields and $custom_meta_fields2


and


function save_custom_meta2($post_id) {

global $custom_meta_fields2;

.....