Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Musilda
Created June 5, 2020 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Musilda/6d8482354983ba5149733d5a544288bb to your computer and use it in GitHub Desktop.
Save Musilda/6d8482354983ba5149733d5a544288bb to your computer and use it in GitHub Desktop.
<?php
add_filter( 'manage_edit-shop_order_sortable_columns', 'musilda_order_sortable_column' );
function musilda_order_sortable_column( $columns ) {
$columns = array( 'order-weight' => 'order-weight' );
return $columns;
}
add_filter( 'pre_get_posts', 'musilda_weight_order_query', 99 );
function musilda_weight_order_query( $query ) {
global $pagenow;
if ( !is_admin() ){
return $query;
}
if ( $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'shop_order' && (!isset( $_GET['orderby'] ) || empty( $_GET['orderby'] ) ) ) {
$query->set( 'orderby', 'ID' );
$query->set( 'order', 'DESC' );
}
$orderby = $query->get( 'orderby' );
if ( 'order-weight' == $orderby ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'order-weigh' );
$query->set( 'meta_query', [] );
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment