I’m a WordPress rookie. I’ve added a custom field to my “Category” taxonomy. The custom field is “custom_order” and its purpose is to hold a number so that my categories can be sorted and displayed in the order I choose.
My problem is that I can’t get them to sort; I can display the categories, and ‘echo’ each category’s “custom_order” number, but I must be missing something when it comes to accessing and sorting the meta_value.
Not sure if I’m going about this the right way either.
Here is my code to add the field to the ‘New Category’ page:
<?php
function taxonomy_add_new_meta_field()
{ ?>
<div class="form-field">
<label for="term_meta[custom_order]">Order</label>
<input type="number" name="term_meta[custom_order]" id="term_meta[custom_order]" value="">
<p class="description">Enter an order number for organizational purposes</p>
</div>
<?php
}
add_action( 'category_add_form_fields', 'taxonomy_add_new_meta_field', 10, 2 );
?>
Here is my code to add the field to the ‘Edit Category’ page:
<?php
function taxonomy_edit_meta_field($term) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_order]">Order</label></th>
<td>
<input type="number" name="term_meta[custom_order]" id="term_meta[custom_order]" value="<?php echo esc_attr( $term_meta['custom_order'] ) ? esc_attr( $term_meta['custom_order'] ) : ''; ?>">
<p class="description">Enter an order number for organizational purposes</p>
</td>
</tr>
<?php
}
add_action( 'category_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2);
?>
Here is my code to save the contents of the field:
<?php
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 2 );
The above snippets of code I got from this website:
http://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/
Last but not least here is my attempt to sort my categories based on this field:
$args = array(
'parent' => $category->cat_ID,
'hide_empty'=>0,
'exclude'=>'1',
'meta_key'=>'custom_order',
'orderby'=>'meta_value_num',
'order'=>'ASC'
);
$subcategories = get_categories($args);
I then iterate through the $subcategories array with a foreach() loop. I thought I had it working until I added a third sub-category; I now see that it puts ‘custom_order’ 1 first, ‘custom_order’ 3 second, and ‘custom_order’ 2 third.
Can anybody lend me a hand?
I would edit the SQL database directly, changing category IDs based on my desired order. Then I would use ‘orderby’=>’id’ to sort.
You could also add an integer to the slug (i.e., 1-fish, 2-chips, 3-dips, etc.) and order by slug, but those would appear on the front end.
Some example workarounds (may need a bit modification):
1st way:
2nd way: