Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created September 29, 2020 00:49
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 jchristopher/2f8cead05c5a8cd49a51e66c9e388096 to your computer and use it in GitHub Desktop.
Save jchristopher/2f8cead05c5a8cd49a51e66c9e388096 to your computer and use it in GitHub Desktop.
Add support for sorting by Name when searching WooCommerce Products in the Admin Area with SearchWP
<?php
add_filter( 'searchwp\query\mods', function( $mods ) {
if ( ! is_admin() ) {
return $mods;
}
if ( empty( $_GET['orderby'] || empty( $_GET['order'] ) ) {
return $mods;
}
if ( 'title' !== $_GET['orderby'] ) {
return $mods;
}
$order = 'asc' === strtolower( $_GET['order'] ) ? 'ASC' : 'DESC';
$mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'product' ) );
$mod->order_by( function( $mod ) {
return $mod->get_local_table_alias() . '.post_name';
}, $order, 1 );
$mods[] = $mod;
return $mods;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment