Few days ago one of my clients asked me to display the "Menu order" of the products in the all products table in the WordPress admin so I ended up with this solution. This can be done with any other data that you want to display in the products table.
```php
/* Add a custom column to the WooCommerce Products table */
function add_menu_order_column($columns) {
// Add the "Menu Order" column
$columns['menu_order'] = __('Menu Order', 'platan');
return $columns;
}
add_filter('manage_edit-product_columns', 'add_menu_order_column', 9999);
/* Populate the "Menu Order" column with the product's menu order value */
function populate_menu_order_column($column, $post_id) {
if ($column === 'menu_order') {
$menu_order = get_post_field('menu_order', $post_id);
echo esc_html($menu_order);
}
}
add_action('manage_product_posts_custom_column', 'populate_menu_order_column', 10, 2);
/* !Make the "Menu Order" column sortable */
function make_menu_order_column_sortable($columns) {
$columns['menu_order'] = 'menu_order';
return $columns;
}
add_filter('manage_edit-product_sortable_columns', 'make_menu_order_column_sortable');
/* Handle sorting by "Menu Order" */
function sort_by_menu_order_in_admin($query) {
if (!is_admin() || !$query->is_main_query() || $query->get('post_type') !== 'product') {
return;
}
$orderby = $query->get('orderby');
if ('menu_order' === $orderby) {
$query->set('orderby', 'menu_order');
$query->set('order', 'ASC'); // Change to DESC if needed
}
}
add_action('pre_get_posts', 'sort_by_menu_order_in_admin');
```