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

Custom taxonomy meta WordPress

  • SOLVED

I have a custom post type of job and a custom taxonomy of client (among others but they're not relevant here) and the client taxonomy has custom meta boxes for when you add a new term.

However when I create a new "client" and add the custom meta info and save it, when I go to edit that client, the custom meta fields aren't visible.

add_action( 'init', 'job_init' );
add_action( 'client_edit_form_fields', 'edit_client', 10, 2 );
add_action( 'client_add_form_fields', 'add_client', 10, 1 );

add_action( 'edited_client', 'save_client', 10, 2);

function job_init()
{
global $wpdb;

$labels = array(
'name' => _x('Jobs', 'post type general name'),
'singular_name' => _x('Job', 'post type singular name'),
'add_new' => _x('Add New', 'Job'),
'add_new_item' => __('Add New Job'),
'edit_item' => __('Edit Jobs'),
'new_item' => __('New Job'),
'view_item' => __('View Jobs'),
'search_items' => __('Search Jobs'),
'not_found' => __('No Jobs found'),
'not_found_in_trash' => __('No Jobs found in Trash'),
'parent_item_colon' => '' );

register_post_type( 'job',
array(
'labels' => $labels,
'label' => __('Jobs'),
'singular_label' => __('Job'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'taxonomies' => array('client','sector','level'),
'supports' => array( 'thumbnail','title','editor')
)
);

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

register_taxonomy('client', 'job',
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'client' ),
));

$wpdb->clientmeta = $wpdb->prefix."clientmeta";
}

function edit_client($tag, $taxonomy)
{
$client_active = get_metadata($tag->taxonomy, $tag->term_id, 'client_active', true);

// Check/Set the default value
if (!$client_active)
$client_active = "Yes";

$client_unit_price = get_metadata($tag->taxonomy, $tag->term_id, 'client_unit_price', true);
$client_ship_price = get_metadata($tag->taxonomy, $tag->term_id, 'client_ship_price', true);

// Check/Set the default value
$client_ship_unit = get_metadata($tag->taxonomy, $tag->term_id, 'client_ship_unit', true);
if (!$client_ship_unit)
$client_ship_unit = 1;

?>
<tr class="form-field">
<th scope="row" valign="top"><label for="client_active">Package Active?</label></th>
<td>
<select name="client_active" id="client_active">
<option value="Yes" selected="selected">Yes</option>
<option value="No" <?php
if ($client_active == "No") echo ' selected="selected";'; ?>>No</option>
</select>
<p class="description">Marking a Package to 'No' will hide all items in that package.</p>
</td>
</tr>

<tr class="form-field">
<th scope="row" valign="top"><label for="client_unit_price">Package Unit Price?</label></th>
<td>
<input type="text" name="client_unit_price" id="client_unit_price"
value="<?php echo $client_unit_price; ?>"/><br />
<p class="description">This is the unit price for the items in this 'package'.</p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="client_ship_price">Package Shipping Price?</label></th>
<td>
<input type="text" name="client_ship_price" id="client_ship_price"
value="<?php echo $client_ship_price; ?>"/><br />
<p class="description">This is the cost per unit to ship the jobs in this package.</p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="client_ship_price">Package Shipping Unit</label></th>
<td>
<input type="text" name="client_ship_unit" id="client_ship_unit"
value="<?php echo $client_ship_unit; ?>"/><br />
<p class="description">This is the quantity of job items. Normally this will be 1. For cards this number may be 6 or 8.</p>
</td>
</tr>
<?php
}

function add_client($taxonomy)
{
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="client_active">Package Active?</label></th>
<td>
<select name="client_active" id="client_active">
<option value="Yes" selected="selected">Yes</option>
<option value="No">No</option>
</select>
<p class="description">Marking a Package to 'No' will hide all items in that package.</p>
</td>
</tr>

<tr class="form-field">
<th scope="row" valign="top"><label for="client_unit_price">Package Unit Price?</label></th>
<td>
<input type="text" name="client_unit_price" id="client_unit_price"
value=""/><br />
<p class="description">This is the unit price for the items in this 'package'.</p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="client_ship_price">Package Shipping Price?</label></th>
<td>
<input type="text" name="client_ship_price" id="client_ship_price"
value=""/><br />
<p class="description">This is the cost per unit to ship the jobs in this package.</p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="client_ship_price">Package Shipping Unit</label></th>
<td>
<input type="text" name="client_ship_unit" id="client_ship_unit"
value=""/><br />
<p class="description">This is the quantity of job items. Normally this will be 1. For cards this number may be 6 or 8.</p>
</td>
</tr>
<?php
}

function save_client($term_id, $tt_id)
{
if (!$term_id) return;

if (isset($_POST['client_active']))
update_metadata($_POST['taxonomy'], $term_id, 'client_active', $_POST['client_active']);

if (isset($_POST['client_unit_price']))
update_metadata($_POST['taxonomy'], $term_id, 'client_unit_price',
sprintf("%01.2f", $_POST['client_unit_price']));

if (isset($_POST['client_ship_price']))
update_metadata($_POST['taxonomy'], $term_id, 'client_ship_price',
sprintf("%01.2f", $_POST['client_ship_price']));

if (!isset($_POST['client_ship_unit']))
$_POST['client_ship_unit'] = 1;
update_metadata($_POST['taxonomy'], $term_id, 'client_ship_unit',
$_POST['client_ship_unit']);
}

Answers (1)

2011-02-11

Maor Barazany answers:

Have you created the table in the database for the clients meta data?

the WordPress meta API for using custom meta data for taxonomies (functions get_metadata , update_metadata etc) needs you to manualy create the table.
You can take the wp_postmeta as the base for the table.

If your taxonomy is 'client' so you have to create the table wp_clientmeta.
The columns will be meta_id, client_id, meta_key and meta_value ,
exactly as the wp_postmeta table's fields are meta_id, post_id, meta_key and meta_value .

You can do it more generally and instead of having a separate table for each taxonomy, to save all of them in a table wp_termmeta.
In such a case, your table's columns will be
meta_id, term_id, meta_key and meta_value

and the argument of the update_metadata and get_metadata will be 'term' instead of
$_POST['taxonomy'] or $tag->taxonomy.

Simply, these functions look for a wp_{argument}meta table, so unless you have thousands of meta data per taxonomy, it is easier to save them all in one wp_termmeta table.


James Beardmore comments:

I've created the table wp_term_taxo_meta, using the simple term meta plugin for simplicity (http://wordpress.org/extend/plugins/simple-term-meta/) which adds the necessary table to the DB. I checked, and its there.

Then I changed

$wpdb->clientmeta = $wpdb->prefix."clientmeta";

to

$wpdb->term_taxo_meta = $wpdb->prefix."term_taxo_meta";

I also changed all $_POST['taxonomy'] to $_POST['term']

and all $tag->taxonomy to $tag->term

as well asfunction add_client($taxonomy) to function add_client($term)

But the term meta is still not being saved to the table on the DB.


James Beardmore comments:

Never mind, this wasn't my issue at all. I changed the code back to the original and double checked my DB table and I hadn't copied it right. Now all works perfectly.


Maor Barazany comments:

Ok, so now everything is working ok and data is saved and retrieved ok from the db?
Is question answered as needed?