Hi there,
I have a custom field, Order (just a text input, into which someone can type a number like "5"), which is associated with a custom taxonomy, Curatorial Category.
The Order column appears on the taxonomy screen. It is not, however, interactive. I wish to click on it to sort the list of terms based on the content of the custom field.
I have followed the tutorial on this webpage:
hidden link
I've added the following code to my site:
// makes custom field sortable
function car_curatorial_category_sortable_columns( $columns ) {
$columns['wpcf_field_homepage-display-order'] = 'wpcf_field_homepage-display-order';
return $columns;
}
add_filter( 'manage_edit-curatorial-category_sortable_columns', 'car_curatorial_category_sortable_columns');
The tutorial above then says the next step is to modify the query using the pre_get_posts() hook. That didn't work for me, and I think it's because pre_get_posts() is for best posts (?).
I found another webpage with instructions specifically related to taxonomies at this address:
https://wordpress.stackexchange.com/questions/159330/custom-taxonomy-custom-sortable-column
Here's the code I tried (from the second response in the web address from the previous line):
function car_curatorial_categories_order_by( $term_query ) {
global $pagenow;
if(!is_admin()) {
return $term_query;
}
if(is_admin() && $pagenow == 'edit-tags.php' && $term_query->query_vars['taxonomy'][0] == 'curatorial-category' && (!isset($_GET['orderby']) || $_GET['orderby'] == 'term_order')) {
// set orderby to the named clause in the meta_query
$term_query->query_vars['orderby'] = 'order_clause';
$term_query->query_vars['order'] = isset($_GET['order']) ? $_GET['order'] : "DESC";
// the OR relation and the NOT EXISTS clause allow for terms without a meta_value at all
$args = array('relation' => 'OR',
'order_clause' => array(
'key' => 'taxonomy_term_order',
'type' => 'NUMERIC'
),
array(
'key' => 'taxonomy_term_order',
'compare' => 'NOT EXISTS'
)
);
$term_query->meta_query = new WP_Meta_Query( $args );
}
return $term_query;
}
add_filter('pre_get_terms', 'car_curatorial_categories_order_by');
The code here used the pre_get_terms() hook, and this appears to be closer to the correct approach. But it still didn't quite work.
Can you offer some guidance?
Thank you.
Saul