$20
Separate custom field write panels for several custom post types
Example: 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!
Jeremy Phillips | 08/03/11 at 5:54pm
| Edit
(5) Possible Answers Submitted...
-

Last edited:
08/03/11
7:00pmGabriel Reguly says:Hi,
Maybe this http://www.youtube.com/watch?v=hv1o6NrINu8&feature=player_embedded could help you.
Regards,
GabrielPrevious versions of this answer: 08/03/11 at 7:00pm
-

Last edited:
08/03/11
7:04pmJoshua Nelson says: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...
- 08/04/11 3:23pm
Jeremy Phillips says:This is working great so far! Gonna keep rolling with it and see if I get stuck on anything. Thanks!
- 08/04/11 3:26pm
Joshua Nelson says: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.
- 08/05/11 2:47pm
Jeremy Phillips says: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?
- 08/05/11 2:50pm
Jeremy Phillips says:^ meant this instead: Works just fine for keys that don't have spaces
- 08/05/11 2:53pm
Joshua Nelson says: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.
- 08/05/11 2:56pm
Joshua Nelson says: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.
- 08/04/11 3:23pm
-

Last edited:
08/03/11
7:24pmRomel Apuya says:you can try the tutorial from this blog..
http://blog.genuineinteractive.com/post/adding-custom-field-gui-to-custom-post-types-wordpress-3.aspx- 08/04/11 11:55am
Romel Apuya says:
- 08/04/11 11:55am
-

Last edited:
08/03/11
7:40pmMilan Petrovic says: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 -

Last edited:
08/04/11
4:01amIvaylo Draganov says:I'd suggest looking into WPAlchemy metabox class and Deluxe Blog Tips' metabox class. There might be something in there for you.
This question has expired.
Jeremy Phillips voted on this question.
Current status of this question: Completed





