Departing from the following tutorial: “Columns in WooCommerce“, the intention is to make the coupon amount column sortable based on amount.
Making it sortable seems to be successful with my already written code. Nevertheless, the rule to sort goes wrong.
I’ve tried several things before but the sort code is not applied after clicking,
Who would like to take a closer look?
What I’ve used so far:
add_filter('manage_edit-shop_coupon_sortable_columns', 'misha_sortable'); function misha_sortable( $sortable_columns ){ $sortable_columns['amount'] = 'amount'; return $sortable_columns; } add_action( 'pre_get_posts', 'misha_filter' ); function misha_filter( $query ) { // if it is not admin area, exit the filter immediately if ( ! is_admin() ) return; if( empty( $_GET['orderby'] ) || empty( $_GET['order'] ) ) return; if( $_GET['orderby'] == 'amount' ) { $query->set('meta_key', 'amount' ); $query->set('orderby', 'meta_value'); // or meta_value_num $query->set('order', $_GET['order'] ); } return $query; }
Advertisement
Answer
The correct metakey is coupon_amount
instead of amount
So this should suffice
// Make column sortable function filter_manage_edit_shop_coupon_sortable_columns( $columns ) { $columns['amount'] = 'amount'; return $columns; } add_filter( 'manage_edit-shop_coupon_sortable_columns', 'filter_manage_edit_shop_coupon_sortable_columns', 10, 1 ); // Fires after the query variable object is created, but before the actual query is run. function action_pre_get_posts( $query ) { // If it is not admin area, exit the filter immediately if( ! is_admin() ) return; // Get orderby $orderby = $query->get( 'orderby' ); // Set query if( $orderby == 'amount' ) { $query->set( 'meta_key', 'coupon_amount' ); $query->set( 'orderby', 'meta_value_num' ); } } add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );