admin管理员组文章数量:1122846
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:
/
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'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?
Share Improve this question asked Apr 1, 2014 at 23:33 jonjon 112 bronze badges 4 |2 Answers
Reset to default 0I 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:
$arguments= array('parent' => $categoryID);
$categories = get_categories( $arguments );
$subcategories=array();
foreach($categories as $each=>$value){
$subcategories[$value->CUSTOM_FIELD_NAME] = $value;
}
asort($subcategories);
foreach($subcategories as $each=>$value){
//do what you want here
}
2nd way:
$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);
本文标签: Trying to sort and display categories(not posts) by custom field 39order39
版权声明:本文标题:Trying to sort and display categories(not posts) by custom field: 'order' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284066a1927170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_categories
, the API knows nothing of your custom meta implementation. you'll have to get all the categories and order them yourself in php. – Milo Commented Apr 1, 2014 at 23:59