Created
September 29, 2020 00:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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