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

Separate custom field write panels for several custom post types WordPress

  • SOLVED

I am working with a site that has been making use of custom fields for a while now. I am creating custom post types that will need additional custom field keys and I would like to have customized write panels for for each post type.

<strong>Example:</strong> I have a custom post of 'review' which needs the following custom fields: album, tracklist and dek. I have a custom post of 'feature', which will also need the custom field key of dek (but I don't want to include the others).

I really don't want to use a plugin, but rather I am looking for a well organized way to register custom fields and designate them to their proper post type allowing certain fields to appear on more than one post type. And yes, I am using custom taxonomies. After I solve this I plan to convert certain fields to taxonomies and have the remaining as is.

Below is the code I am currently using to register my custom fields for posts and my review cpt:


/* Add meta box for adding release details to a post */

add_action('admin_init','meta_release_init');

function meta_release_init()
{
// review the function reference for parameter details
// http://codex.wordpress.org/Function_Reference/add_meta_box

// add a meta box for each of the wordpress page types: posts and pages
foreach (array('post','review') as $type)
{
add_meta_box('meta_release', 'Release Details', 'meta_release_setup', $type, 'normal', 'high');
}

// add a callback function to save any data a user enters in
add_action('save_post','meta_release_save');
}

function meta_release_setup()
{
global $post;

$release['Album Title'] = get_post_meta($post->ID,'Album Title',TRUE);
$release['Artist Name'] = get_post_meta($post->ID,'Artist Name',TRUE);
$release['Label'] = get_post_meta($post->ID,'Label',TRUE);
$release['Year'] = get_post_meta($post->ID,'Year',TRUE);
$release['Artist Website'] = get_post_meta($post->ID,'Artist Website',TRUE);
$release['Tracklist'] = get_post_meta($post->ID,'Tracklist',TRUE);

include(TEMPLATEPATH . '/post-meta-release.php');

// create a custom nonce for submit verification later
echo '<input type="hidden" name="meta_release_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}

function meta_release_save($post_id)
{
// authentication checks
// make sure data came from our meta box
if (!wp_verify_nonce($_POST['meta_release_noncename'],__FILE__)) return $post_id;
// check user permissions
if ($_POST['post_type'] == 'page')
{
if (!current_user_can('edit_page', $post_id)) return $post_id;
}
else
{
if (!current_user_can('edit_post', $post_id)) return $post_id;
}
// authentication passed, save data
// var types
// single: _my_meta[var]
// array: _my_meta[var][]
// grouped array: _my_meta[var_group][0][var_1], _my_meta[var_group][0][var_2]
foreach ($_POST['release'] as $field => $value)
{
$new_data = $value;
if ($new_data != '')
{
update_post_meta($post_id, $field, esc_attr($new_data));
}
else
{
delete_post_meta($post_id,$field);
}
}
return $post_id;
}


Thanks for your answers!

Answers (5)

2011-08-03

Joshua Nelson answers:

I'd try using Bill Erickson's meta boxes tutorial: http://www.billerickson.net/wordpress-metaboxes/

You have to download and install the php library, but it's super easy. Then you can add meta boxes to any post type you like. It really simplifies the whole meta box issue and makes it a lot easier to dictate type, placement, values, etc...


Jeremy Phillips comments:

This is working great so far! Gonna keep rolling with it and see if I get stuck on anything. Thanks!


Joshua Nelson comments:

Great! Let me know if you get stuck, I can help out with the specific code, too - I've used this for multiple custom post types before.


Jeremy Phillips comments:

Well, all is well except for one issue. I am working with meta keys that were registered with capital letters and spaces. When I try to save any of the values for them they don't stick. Works just fine for values that don't have spaces. Have you come across this before?


Jeremy Phillips comments:

^ meant this instead: <em>Works just fine for <strong>keys</strong> that don't have spaces</em>


Joshua Nelson comments:

Yea, you should be registering everything in lower case and using underscores instead of spaces - that's general operating procedure for wordpress stuff. You might have to change them, just be sure to back up everything if you already have assigned values to them. If you have, you can go into your sql database, fine the table and change the values to the correct, updated meta key.


Joshua Nelson comments:

Also, the id of the key is what you're using to pull it up, right? that's the one that can't be uppercase or include spaces. The title/name, however, can include spaces and capital letters - that's the part that's displayed and where it matters.

Let me know if you have any other issues.

2011-08-03

Gabriel Reguly answers:

Hi,

Maybe this [[LINK href="http://www.youtube.com/watch?v=hv1o6NrINu8&feature=player_embedded"]]http://www.youtube.com/watch?v=hv1o6NrINu8&feature=player_embedded[[/LINK]] could help you.

Regards,
Gabriel

2011-08-03

Romel Apuya answers:

you can try the tutorial from this blog..

[[LINK href="http://blog.genuineinteractive.com/post/adding-custom-field-gui-to-custom-post-types-wordpress-3.aspx"]]http://blog.genuineinteractive.com/post/adding-custom-field-gui-to-custom-post-types-wordpress-3.aspx[[/LINK]]


Romel Apuya comments:

[[LINK href="http://wptheming.com/2010/08/custom-metabox-for-post-type/"]]http://wptheming.com/2010/08/custom-metabox-for-post-type/[[/LINK]]

2011-08-03

Milan Petrovic answers:

My GD Custom Posts And Taxonomies Tools Pro can do all that for you without any coding with full support for custom meta boxes and powerful meta box/custom fields editor and integration with default or custom post types:

http://d4p.me/gdtt

2011-08-04

Ivaylo Draganov answers:

I'd suggest looking into [[LINK href="http://www.farinspace.com/wpalchemy-metabox/"]]WPAlchemy metabox class[[/LINK]] and [[LINK href="http://www.deluxeblogtips.com/meta-box-script-for-wordpress/"]]Deluxe Blog Tips' metabox class[[/LINK]]. There might be something in there for you.