Created a CPT ("officers"), and registered a custom taxonomy ("officerspos") for that CPT. I have excluded the CPT code below as there is no problem with it.
The custom meta data field shows up fine on both the add and edit screens (I'm wanting to use it for sorting purposes)
I can't however seem to get the values to save and retrieve.
Codes as follows:
Register taxonomy
//Register custom taxonomy' (3 of them below) NOTE: hierarchical true makes them select boxes
register_taxonomy("officerspos", array("officers"),
array("hierarchical" => true,
"label" => "Officer Positions",
"singular_label" => "Officer Position",
"rewrite" => true,
));
Add sort field to add taxonomy screen
function officers_taxonomy_meta_field() {
// this will add the custom meta field Sorting Order to the Add new taxonomy page for officers postions
?>
<div class="form-field">
<label for="sort_meta[custom_sort_meta]">Sorting Order</label>
<input type="text" name="sort_meta[custom_sort_meta]" id="sort_meta[custom_sort_meta]" value="">
<p class="description">Numeric Value 1 to 1000, 1 being the top of the sort order.</p>
</div>
<?php
}
add_action( 'officerspos_add_form_fields', 'officers_taxonomy_meta_field', 10, 2 );
Add sort field to edit taxonomy screen
function officers_taxonomy_edit_meta_field($sort) {
// put the sort ID into a variable
$s_id = $sort->sort_id;
// retrieve the existing value(s) for this meta field. This returns an array
$sort_meta = get_option( "taxonomy_$s_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="sort_meta[custom_sort_meta]">Sorting Order</label></th>
<td>
<input type="text" name="sort_meta[custom_sort_meta]" id="sort_meta[custom_sort_meta]" value="<?php echo esc_attr( $sort_meta['custom_sort_meta'] ) ? esc_attr( $sort_meta['custom_sort_meta'] ) : ''; ?>">
<p class="description">Numeric Value 1 to 1000, 1 being the top of the sort order.</p>
</td>
</tr>
<?php
}
add_action( 'officerspos_edit_form_fields', 'officers_taxonomy_edit_meta_field', 10, 2 );
Save taxonomy fields callback function
function save_taxonomy_custom_meta( $sort_id ) {
if ( isset( $_POST['sort_meta'] ) ) {
$s_id = $sort_id;
$sort_meta = get_option( "taxonomy_$s_id" );
$cat_keys = array_keys( $_POST['sort_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['sort_meta'][$key] ) ) {
$sort_meta[$key] = $_POST['sort_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$s_id", $sort_meta );
}
}
add_action( 'edited_officerspos', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_officerspos', 'save_taxonomy_custom_meta', 10, 2 );
any help on why this won't save and recall would be greatly appreciated.