Skip to content

Instantly share code, notes, and snippets.

@hamidrezayazdani
Last active November 18, 2023 14:34
Show Gist options
  • Save hamidrezayazdani/692e4ed155c69651bd39a4b1a75661a3 to your computer and use it in GitHub Desktop.
Save hamidrezayazdani/692e4ed155c69651bd39a4b1a75661a3 to your computer and use it in GitHub Desktop.
Change WooCommerce order by condition on the edit product screen.
<?php
/**
* Change views on the edit product screen.
*
* @param array $views Array of views.
* @return array
*/
function ywp_product_views( $views ) {
global $wp_query;
// Add sorting link.
if ( current_user_can( 'edit_others_products' ) ) {
$class = ( isset( $wp_query->query['orderby'] ) && 'id' === $wp_query->query['orderby'] ) ? 'current' : '';
$query_string = remove_query_arg( array( 'orderby', 'order' ) );
$query_string = add_query_arg( 'orderby', rawurlencode( 'id' ), $query_string );
$query_string = add_query_arg( 'order', rawurlencode( 'ASC' ), $query_string );
$views['byorder'] = '<a href="' . esc_url( $query_string ) . '" class="' . esc_attr( $class ) . '">' . __( 'Sorting', 'woocommerce' ) . '</a>';
}
return $views;
}
add_filter( 'views_edit-product', 'ywp_product_views', 9999 );
// The code goes to your active theme/child theme functions.php file
// NOTE: ASC = Ascending, DESC = descending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment