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

Help with custom post type 'Marketplace Items' setup WordPress

  • SOLVED

I would like to set up a custom post type called 'Marketplace Items' (let me know if keeping this to one word such as Marketplace would be preferable). I have a good start but am not quite there. I need them to have a hierarchical category structure just like pages and need to add a few meta boxes (in the main portion of the admin not the right side) and have them store data. I would love to get a query for the front-end as well! I have a pastebin of my start here:

http://pastebin.com/he7i08N9

<strong>The meta boxes that I need (just simple text fields) are:</strong>

member name
retailer name
manufacturer name
SKU
product name (dont need a meta box for this one- can just be the title field)
retail price
sale price
description (dont need a meta box for this one- can just be the main edit textfield)
image URL
buy now URL

Answers (4)

2011-02-24

Pippin Williamson answers:

Looks like you've just about gotten everything you need except the custom meta, so here's the complete custom meta function(s) for you :)


/* CSUTOM META BOX */

$prefix = 'mi_';

$meta_box = array(
'id' => 'marketplace-meta',
'title' => 'Item Information',
'page' => 'Marketplace', // replace this with your post type name
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Member Name',
'id' => $prefix . 'member',
'type' => 'text',
'desc' => 'Member name',
),
array(
'name' => 'Retailer Name',
'id' => $prefix . 'retail_name',
'type' => 'text',
'desc' => 'Retailer name',
),
array(
'name' => 'Manufacturer Name',
'id' => $prefix . 'manufacturer',
'type' => 'text',
'desc' => 'Manufacturer name',
),
array(
'name' => 'Retail Price',
'id' => $prefix . 'price',
'type' => 'text',
'desc' => 'Retail Price',
),
array(
'name' => 'Image URL',
'id' => $prefix . 'image',
'type' => 'text',
'desc' => 'Image URL',
),
array(
'name' => 'Buy Now URL',
'id' => $prefix . 'buy_now',
'type' => 'text',
'desc' => 'Buy Now URL',
),
)
);

add_action('admin_menu', 'custom_meta_add_box');
function custom_meta_add_box() {
global $meta_box;

add_meta_box($meta_box['id'], $meta_box['title'], 'custom_meta_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}

// Callback function to show fields in meta box
function custom_meta_show_box() {
global $meta_box, $post;

// Use nonce for verification
echo '<input type="hidden" name="custom_meta_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: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%" />', '
', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '
', $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', 'custom_meta_save_data');

// Save data from meta box
function custom_meta_save_data($post_id) {
global $meta_box;

// verify nonce
if (!wp_verify_nonce($_POST['custom_meta_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']];

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


Adam Bundy comments:

Could you please place this code into the other init code I posted where it should go? Please comment places I should change any theme-specific bits to the name of my theme (twentyten). Also, the categories are not working with the current code I posted- please take a look at that as well. Bonus points for the front end query too!

Thanks!


Pippin Williamson comments:

Here you go, this should be the complete code needed.

http://pastebin.com/Vitbw8fu


Adam Bundy comments:

Pippin, thanks much! I have reset wordpress and re-run this, and it looks like there might be some errors somewhere- looks like around line 200 of your code in pastebin might have an error- Im not getting the meta boxes or categories. Should this go at the end of functions.php? Im using the out-of-the-box functions.php from 2010 and just putting it at the end. Thanks!


Pippin Williamson comments:

I've just looked through and didn't find any errors, can you give me the error it spat out?

And the end of functions.php is fine.


Adam Bundy comments:

Ive just sent you a private message Pippin. Thanks for your help!

2011-02-24

Cosmin Popovici answers:

For the metaboxes, this should to the trick

<?php

// Custom fields for WP write panel

$mt_metaboxes = array(
"member-name" => array (
"name" => "member-name",
"default" => "",
"label" => "Member Name",
"type" => "text",
),
"retailer-name" => array (
"name" => "retailer-name",
"default" => "",
"label" => "Rretailer name",
"type" => "text",
),
"manufacturer-name" => array (
"name" => "manufacturer-name",
"default" => "",
"label" => "Manufacturer name",
"type" => "text",
),
"sku" => array (
"name" => "sku",
"default" => "",
"label" => "SKU",
"type" => "text",
),
"retail-price" => array (
"name" => "retail-price",
"default" => "",
"label" => "Retail Price",
"type" => "text",
),
"sale-price" => array (
"name" => "sale-price",
"default" => "",
"label" => "Sale Price",
"type" => "text",
),
"image-url" => array (
"name" => "image-url",
"default" => "",
"label" => "Image URL",
"type" => "text",
),
"buy-now" => array (
"name" => "buy-now",
"default" => "",
"label" => "Buy now URL",
"type" => "text",
),

);

function mytheme_meta_box_content() {
global $post, $mt_metaboxes;
echo '<table>'."\n";
foreach ($mt_metaboxes as $mt_metabox) {
$mt_metaboxvalue = get_post_meta($post->ID,$mt_metabox["name"],true);
if ($mt_metaboxvalue == "" || !isset($mt_metaboxvalue)) {
$mt_metaboxvalue = $mt_metabox['default'];
}
echo "\t".'<tr>';
echo "\t\t".'<th style="text-align: right;"><label for="'.$mt_metabox.'">'.$mt_metabox['label'].':</label></th>'."\n";
echo "\t\t".'<td><input size="50" type="'.$mt_metabox['type'].'" value="'. stripslashes($mt_metaboxvalue).'" name="mytheme_'.$mt_metabox["name"].'" id="'.$mt_metabox.'"/></td>'."\n";
echo "\t".'</tr>'."\n";
}
echo '</table>'."\n\n";
}

function mytheme_metabox_insert($pID) {
global $mt_metaboxes;
foreach ($mt_metaboxes as $mt_metabox) {
$var = "mytheme_".$mt_metabox["name"];
if (isset($_POST[$var])) {
add_post_meta($pID,$mt_metabox["name"],$_POST[$var],true) or update_post_meta($pID,$mt_metabox["name"],$_POST[$var]);
}
}
}

function mytheme_meta_box() {
if ( function_exists('add_meta_box') ) {
add_meta_box('woothemes-settings','Additional Fields (Optional)','mytheme_meta_box_content','post','normal');
add_meta_box('woothemes-settings','Additional Fields (Optional)','mytheme_meta_box_content','page','normal');
}
}

add_action('admin_menu', 'mytheme_meta_box');
add_action('wp_insert_post', 'mytheme_metabox_insert');

?>


Adam Bundy comments:

Could you please edit this to be specific to my post type name and place it into the other init code I posted where it should go? Please comment where I should change the 'mytheme_' to the name of my theme (twentyten). Also, the categories are not working with the current code I posted- please take a look at that as well. Bonus points for the front end query too!

Thanks!

2011-02-24

Vidyut Kale answers:

The above code looks good, but I really recommend using a plugin like headspace2 (for example) for doing this, unless you are reasonably certain that you will not be changing themes. The reason is that if you are using the theme prefix on the meta boxes, the data will become unavailable when you change it and you will have to import the tags to the new one. In any case, you will have to copy the code to there (and remember it and update it if you end up changing the theme after three years, etc. If you change more than a few times, it can get really painful.

Better is to set up your post types, taxonomies, meta data, etc through a plugin, so that you are relatively future-proof.

Plus, its a million times easier.


Adam Bundy comments:

Thanks Vidyut, I considered using Custom Post Type UI or other plugin for this but would like to learn how to create them via the functions file, and the twentyten case here is just a testing environment to get the post type working correctly- the destination project for this is a totally custom theme that wont be changing. Thanks!

2011-02-24

Victor Teixeira answers:

Well, O would recommend the Fields plugin (http://wordpress.org/extend/plugins/fields/), so it's really easy to setup custom meta boxes any way you want and whenever you want.

To display a field on the site you just call it like this: field_meta('your_field_name');

Or you can use the WP Alchemy metabox class: http://farinspace.com/wpalchemy-metabox/


Adam Bundy comments:

Thanks Victor, but this time I'd really like to create these manually. I appreciate it!