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

custom post type with meta box and custom field taxonomy WordPress

  • SOLVED

Im looking for a solution to this.

i want the meta boxes on the custom post type screen, instead of using the array of options like it is now, to populate for each term, so for the genre meta box, have it get a list of genre terms, and so on for all the custom fields.

also on the post screen i dont want to list each term box on the right, just want the tags and categories for the post type to be there.

here is the code on pastebin

http://pastebin.com/zXBdtcD0

here is the source here


<?php
/*
Plugin Name: Gamer Press
Description: Custom Gaming Plugin for Gamer Review Site
Author: Chris McCoy
*/

gamerpress_plugincheck();

function gamerpress_plugincheck() {
$plugins = get_option('active_plugins');
$required = 'wp-postratings/wp-postratings.php';
if (!in_array( $required,$plugins)) {
add_thickbox();
add_action('admin_notices', 'gamerpress_check_notice');
}
}

function gamerpress_check_notice() { ?>
<div class='updated fade'>
<p>The Post Rating plugin is required for this plugin to function properly. <a href="<?php echo admin_url('plugin-install.php?tab=plugin-information&plugin=wp-postratings&TB_iframe=true&width=640&height=517'); ?>" class="thickbox onclick">Install now</a>.</p>
</div>
<?php
}

function gamerpress_js() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_enqueue_script('jquery');
wp_register_script('custom_table', WP_PLUGIN_URL . '/gamerpress/js/beautiful-data.js',array('jquery'),'1.0');
wp_enqueue_script('custom_table');
wp_register_script('custom_init', WP_PLUGIN_URL . '/gamerpress/js/init.js',array('jquery'),'1.0');
wp_enqueue_script('custom_init');
}
}

add_action('init','gamerpress_js');

function gamerpress_css() {
wp_register_style('gamerpress_style', WP_PLUGIN_URL . '/gamerpress/gamerpress.css');
wp_enqueue_style('gamerpress_style');
}

add_action('wp_print_styles','gamerpress_css');

function setup_custom_post_types()
{

$review_item_labels = array
(
'name' => __('Reviews'),
'singular_name' => __('Reviews'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New Review'),
'edit_item' => __('Edit Review'),
'new_item' => __('New Review'),
'view_item' => __('View Review'),
'search_items' => __('Search Reviews'),
'not_found' => __('No Reviews found'),
'not_found_in_trash' => __('No Reviews found in Trash'),
'parent_item_colon' => ''
);
$review_item_args = array
(
'label' => __('Reviews'),
'labels' => $review_item_labels,
'description' => __('Reviews'),
'public' => TRUE,
'hierarchical' => TRUE,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'page-attributes'),
'register_meta_box_cb' => 'review_item_meta_box',
'menu_position' => 5,
'menu_icon' => get_bloginfo('template_directory').'/images/review.png',
'rewrite' => array('slug' => 'reviews'),
'show_in_nav_menus' => TRUE
);
register_post_type('reviews', $review_item_args);

$review_item_category_args = array
(
'labels' => array('name' => __('Review Categories')),
'hierarchical' => TRUE,
'rewrite' => array('slug' => 'review-category')
);
register_taxonomy('reviews-category', array('reviews'), $review_item_category_args);

$review_item_tag_args = array
(
'labels' => array('name' => __('Review Tags')),
'show_in_nav_menus' => FALSE,
'rewrite' => array('slug' => 'review-tag')
);
register_taxonomy('reviews-tag', array('reviews'), $review_item_tag_args);

$genre_label = array(
'name' => _x( 'Genre', 'taxonomy general name' ),
'singular_name' => _x( 'genre', 'taxonomy singular name' ),
'search_items' => __( 'Search genre' ),
'all_items' => __( 'All Genre' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
);

register_taxonomy('genre',array('reviews'), array(
'hierarchical' => true,
'labels' => $genre_label,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
));

$genreterms = array('Sci-Fi', 'Fantasy', 'Shooter','Sports','Racing','MMORPG');
foreach ($genreterms as $genreterm) {
wp_insert_term($genreterm,'genre');
}

$type_label = array(
'name' => _x( 'Type', 'taxonomy general name' ),
'singular_name' => _x( 'type', 'taxonomy singular name' ),
'search_items' => __( 'Search Type' ),
'all_items' => __( 'All Type' ),
'parent_item' => __( 'Parent Type' ),
'parent_item_colon' => __( 'Parent Type:' ),
'edit_item' => __( 'Edit Type' ),
'update_item' => __( 'Update Type' ),
'add_new_item' => __( 'Add New Type' ),
'new_item_name' => __( 'New Type Name' ),
);

register_taxonomy('type',array('reviews'), array(
'hierarchical' => true,
'labels' => $type_label,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
));

$typeterms = array('Sci-Fi', 'Fantasy', 'Shooter','Sports','Racing','MMORPG');
foreach ($typeterms as $typeterm) {
wp_insert_term($typeterm,'type');
}

$score_label = array(
'name' => _x( 'Score', 'taxonomy general name' ),
'singular_name' => _x( 'score', 'taxonomy singular name' ),
'search_items' => __( 'Search Score' ),
'all_items' => __( 'All Score' ),
'parent_item' => __( 'Parent Score' ),
'parent_item_colon' => __( 'Parent Score:' ),
'edit_item' => __( 'Edit Score' ),
'update_item' => __( 'Update Score' ),
'add_new_item' => __( 'Add New Score' ),
'new_item_name' => __( 'New Score Name' ),
);

register_taxonomy('score',array('reviews'), array(
'hierarchical' => true,
'labels' => $score_label,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'score' ),
));

$scoreterms = array('Poor', 'Low', 'Fair', 'Medium', 'Great', 'Excellent');
foreach ($scoreterms as $scoreterm) {
wp_insert_term($scoreterm,'score');
}

$xprate_label = array(
'name' => _x( 'XP Rate', 'taxonomy general name' ),
'singular_name' => _x( 'xprate', 'taxonomy singular name' ),
'search_items' => __( 'Search XP Rate' ),
'all_items' => __( 'All XP Rate' ),
'parent_item' => __( 'Parent XP Rate' ),
'parent_item_colon' => __( 'Parent XP Rate:' ),
'edit_item' => __( 'Edit XP Rate' ),
'update_item' => __( 'Update XP Rate' ),
'add_new_item' => __( 'Add New XP Rate' ),
'new_item_name' => __( 'New XP Rate Name' ),
);

register_taxonomy('xprate',array('reviews'), array(
'hierarchical' => true,
'labels' => $xprate_label,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'xprate' ),
));

$xprateterms = array('Low', 'Medium', 'High');
foreach ($xprateterms as $xprateterm) {
wp_insert_term($genreterm,'xprate');
}

$playerbase_label = array(
'name' => _x( 'Player Base', 'taxonomy general name' ),
'singular_name' => _x( 'playerbase', 'taxonomy singular name' ),
'search_items' => __( 'Search Player Base' ),
'all_items' => __( 'All Player Base' ),
'parent_item' => __( 'Parent Player Base' ),
'parent_item_colon' => __( 'Parent Player Base:' ),
'edit_item' => __( 'Edit Player Base' ),
'update_item' => __( 'Update Player Base' ),
'add_new_item' => __( 'Add New Player Base' ),
'new_item_name' => __( 'New Player Base Name' ),
);

register_taxonomy('playerbase',array('reviews'), array(
'hierarchical' => true,
'labels' => $playerbase_label,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'playerbase' ),
));

$playerbaseterms = array('Low', 'Medium', 'High');
foreach ($playerbaseterms as $playerbaseterm) {
wp_insert_term($playerbaseterm,'playerbase');
}
}

add_action('init','setup_custom_post_types');

add_action('admin_menu', 'review_item_info');

function review_item_info() {
add_submenu_page('edit.php?post_type=reviews', __('Info'), __('Info'), 'manage_options', 'reviews_info', 'reviews_information');
}

function reviews_information() { ?>

<div class="wrap">
<h2>Gamer Press Information</h2>

<p>
Gamer Press gaming review addon for wordpress 3.0.1.
</p>

</div>

<?php

}

$prefix = 'gamer_';

$meta_box = array(
'id' => 'my-meta-box',
'title' => 'Gamer Press ::',
'page' => 'reviews',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Game Title',
'id' => $prefix . 'title',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Type',
'id' => $prefix . 'type',
'type' => 'select',
'options' => array('Sci-Fi', 'Fantasy', 'Shooter','Sports','Racing','MMORPG')
),

array(
'name' => 'Genre',
'id' => $prefix . 'genre',
'type' => 'select',
'options' => array('Sci-Fi', 'Fantasy', 'Shooter','Sports','Racing','MMORPG')
),

array(
'name' => 'Playerbase',
'id' => $prefix . 'playerbase',
'type' => 'select',
'options' => array('Low', 'Medium', 'High')
),

array(
'name' => 'XP Rate',
'id' => $prefix . 'xprate',
'type' => 'select',
'options' => array('Low', 'Medium', 'High')
),

array(
'name' => 'Score',
'id' => $prefix . 'score',
'type' => 'select',
'options' => array('Poor', 'Low', 'Fair', 'Medium', 'Great', 'Excellent')
)
)
);

function review_item_meta_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'review_item_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}

function review_item_show_box() {
global $meta_box, $post;

echo '<input type="hidden" name="review_item_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

echo '<table class="form-table">';

foreach ($meta_box['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);

echo '<tr>',
'<th style="width:20%"><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;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
'<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}

echo '</table>';
}

add_action('save_post', 'review_item_save_data');

function review_item_save_data($post_id) {
global $meta_box;

if (!wp_verify_nonce($_POST['review_item_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}

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']];

if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}

function review_item_showdata() {
global $meta_box, $post;
foreach ($meta_box['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
echo $meta;
}
}

function gamerlist_shortcode($content) {
global $post;
?>

<?php $other = new WP_query('showposts=-1&orderby=rand&meta_key=gamer_score');?>
<?php if ($other->have_posts()) : ?>

<div id="data" class="beautifulData">
<table>

<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Genre</th>
<th>Playerbase</th>
<th>XP Rate</th>
<th>Score</th>
<th>Rating</th>
</tr>
</thead>

<tbody>

<?php global $wp_query; $wp_query->in_the_loop = true; ?>
<?php while ($other->have_posts()) : $other->the_post(); ?>

<tr>
<td><a href="<?php the_permalink();?>"><?php the_title();?></a></td>
<td><?php echo get_post_meta($post->ID, 'gamer_type', true);?></td>
<td><?php echo get_post_meta($post->ID, 'gamer_genre', true);?></td>
<td><?php echo get_post_meta($post->ID, 'gamer_playerbase', true);?></td>
<td><?php echo get_post_meta($post->ID, 'gamer_xprate', true);?></td>
<td><?php echo get_post_meta($post->ID, 'gamer_score', true);?></td>
<td>3.89</td>
</tr>

<?php endwhile; ?>
<?php endif; ?>

</tbody>
</table>

</div>

<?php
}

add_shortcode('gamerlist','gamerlist_shortcode');

?>


Answers (2)

2010-11-25

Tom Ransom answers:

I guess I am confused. Why are you creating taxonomies during your setup and then turn around and use a meta box with custom fields to store the same information.

Why not just use the taxonomy fields during the custom post?

If you want to control the presentation of the available terms by taxonomy - the get_terms function will let you select list from the terms already in the database. (You'll also want to hide the taxonomy meta boxes)

Then instead of storing the chosen term in a custom field - you would use wp_set_object_terms to create the relationship between your custom post object and the selected terms/taxonomies.


chrismccoy comments:

i was doing a regular post, with custom fields before, so thats why the meta boxes are like that (taken from before i had a custom post type), i want to convert them to the new custom post types, without custom fields.


Tom Ransom comments:

Here is the revised code: http://pastebin.com/sAFnbgeb

I modified your meta_fields table to add a new type 'taxonomy'
I hid the UI for the taxonomies - other than category and tags
There is a new function that compares the selected terms with the available terms in a taxonomy to get the dropdown select list.
The save_post_data function only needs to save the title - WordPress does the rest of the work.


chrismccoy comments:

2 things.

i want to show the terms on the ui, just not on the post page on the right, so i can add more for each, genre, playerbase, etc.

and i need the gamerlist shortcode modified for a query for custom post type.

if you can do those 2 things, then i will award the funds.

thx!


Tom Ransom comments:

Just change the value of show-ui in each taxonomy to true.

then Add -
remove_meta_box('XXXdiv', 'post', 'normal');
where XXX is the taxonomy id (e.g. xprate) - this will remove the boxes from the edit post page but keep them in the admin sidebar

fix your query by replacing the term meta_key=gamer_score with taxonomy=score

These are both beyond the scope of your first question FWIW.


chrismccoy comments:

ok almost done, for the query i added that, is their anything i need to change to query the variables?

can you post some code ?

thx

2010-11-26

rilwis answers:

<strong>This plugin has already registered meta box for "review" post type</strong>. When I tried it on my theme, the meta box didn't show. But when I changed theme to TwentyTen, it worked! If your meta box doesn't show at all, maybe the problem is your theme. Try another theme for testing. (see the meta box in my attached image)

To hide boxes on the right, you can just deselect them in Screen Options (on the top of screen). This way let you use these settings later. But if you really want to <strong>remove all of these boxes</strong>, you can comment out lines from 94 to 223 (end of function) in the plugin, like this:

function setup_custom_post_types()
{

$review_item_labels = array
(
'name' => __('Reviews'),
'singular_name' => __('Reviews'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New Review'),
'edit_item' => __('Edit Review'),
'new_item' => __('New Review'),
'view_item' => __('View Review'),
'search_items' => __('Search Reviews'),
'not_found' => __('No Reviews found'),
'not_found_in_trash' => __('No Reviews found in Trash'),
'parent_item_colon' => ''
);
$review_item_args = array
(
'label' => __('Reviews'),
'labels' => $review_item_labels,
'description' => __('Reviews'),
'public' => TRUE,
'hierarchical' => TRUE,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'page-attributes'),
'register_meta_box_cb' => 'review_item_meta_box',
'menu_position' => 5,
'menu_icon' => get_bloginfo('template_directory').'/images/review.png',
'rewrite' => array('slug' => 'reviews'),
'show_in_nav_menus' => TRUE
);
register_post_type('reviews', $review_item_args);

$review_item_category_args = array
(
'labels' => array('name' => __('Review Categories')),
'hierarchical' => TRUE,
'rewrite' => array('slug' => 'review-category')
);
register_taxonomy('reviews-category', array('reviews'), $review_item_category_args);

$review_item_tag_args = array
(
'labels' => array('name' => __('Review Tags')),
'show_in_nav_menus' => FALSE,
'rewrite' => array('slug' => 'review-tag')
);
register_taxonomy('reviews-tag', array('reviews'), $review_item_tag_args);

/* COMMENT OUT FROM HERE

$genre_label = array(
'name' => _x( 'Genre', 'taxonomy general name' ),
'singular_name' => _x( 'genre', 'taxonomy singular name' ),
'search_items' => __( 'Search genre' ),
'all_items' => __( 'All Genre' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
);

......
......

$playerbaseterms = array('Low', 'Medium', 'High');
foreach ($playerbaseterms as $playerbaseterm) {
wp_insert_term($playerbaseterm,'playerbase');
}
*/ // TO THE END OF THIS FUNCTION
}