Sorting Taxonomy Terms in WordPress Admin by a Custom Field
Managing a large taxonomy like product categories or blog tags can be challenging, especially when you need to control the order they appear on your website. WordPress provides a default sorting option within the taxonomy terms list, but what if you need to sort based on a custom field you've added? This article will guide you through the process of implementing custom sorting for taxonomy terms using a custom field.
The Problem:
You've added a custom field to your taxonomy terms (e.g., "order" or "priority") and need to control the order in which these terms appear in the admin panel. WordPress doesn't automatically sort by this custom field.
The Solution: Custom Sorting Function
The solution involves creating a custom function that hooks into WordPress's term sorting process. This function will retrieve the custom field value for each term and use it to determine the order in which the terms are displayed.
Code Example:
<?php
// Register custom field for taxonomy terms
add_action( 'product_cat_add_form_fields', 'add_product_cat_order_field', 10, 2 );
function add_product_cat_order_field( $taxonomy, $term ) {
$order = get_term_meta( $term->term_id, 'order', true ); ?>
<div class="form-field term-order-wrap">
<label for="order">Order:</label>
<input type="number" name="order" id="order" value="<?php echo esc_attr( $order ); ?>">
<p class="description">Enter a number to set the order of this term.</p>
</div>
<?php }
// Save custom field data
add_action( 'edited_product_cat', 'save_product_cat_order_field' );
add_action( 'create_product_cat', 'save_product_cat_order_field' );
function save_product_cat_order_field( $term_id ) {
if ( isset( $_POST['order'] ) ) {
update_term_meta( $term_id, 'order', sanitize_text_field( $_POST['order'] ) );
}
}
// Custom sorting function
add_filter( 'manage_edit-product_cat_columns', 'add_order_column_to_term_table' );
function add_order_column_to_term_table( $columns ) {
$columns['order'] = __( 'Order', 'your-textdomain' );
return $columns;
}
add_filter( 'manage_edit-product_cat_sortable_columns', 'make_order_column_sortable' );
function make_order_column_sortable( $columns ) {
$columns['order'] = 'order';
return $columns;
}
add_action( 'pre_get_terms', 'sort_terms_by_custom_field' );
function sort_terms_by_custom_field( $query ) {
if ( is_admin() && $query->is_main_query() && $query->get( 'taxonomy' ) == 'product_cat' && isset( $_GET['orderby'] ) && $_GET['orderby'] == 'order' ) {
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'order' );
$query->set( 'order', 'ASC' ); // Modify order (ASC/DESC) if needed
}
}
Explanation:
- Register Custom Field: This code adds an "Order" field to your taxonomy term edit form.
- Save Custom Field Data: This code saves the "Order" value to the term's metadata when a term is created or updated.
- Custom Sorting Function:
- Adds an "Order" column to the taxonomy terms list.
- Makes the "Order" column sortable.
- Hooks into the
pre_get_terms
action and modifies the query to sort terms by the "order" meta key.
Key Points:
- Taxonomy: Replace "product_cat" with the specific taxonomy you're targeting (e.g., 'category', 'post_tag').
- Field Name: Adjust the field name ("order") to match your custom field.
- Order: Set the order to 'ASC' (ascending) or 'DESC' (descending) based on your needs.
Implementing the Solution:
- Create a Plugin: Create a new plugin file (e.g.,
custom-taxonomy-sorting.php
) and add the code above. - Activate the Plugin: Activate your plugin through the Plugins menu in WordPress.
- Add Order Fields: Edit your taxonomy terms and enter the desired "Order" values in the new field.
- Sort Terms: Visit the taxonomy terms list in the admin panel and use the new "Order" column to sort terms.
Additional Considerations:
- Default Sorting: If you don't want to rely on the custom field for sorting, consider adding default sorting to the terms list.
- Caching: When using a custom field for sorting, ensure that your caching plugins are configured properly to avoid conflicts.
- Performance: For large taxonomies, consider optimizing your queries to improve performance.
Conclusion:
By adding a custom sorting function, you can easily control the order of your taxonomy terms in the WordPress admin panel. This provides greater flexibility and allows you to organize your website's content based on your specific needs.