Created
May 9, 2019 17:12
-
-
Save thetwopct/e2aef4ca9c8eff4be2bb1286da8770f1 to your computer and use it in GitHub Desktop.
Custom Orderby
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
function theme_modify_the_main_query( $query ) { | |
if ( isset( $_GET['orderby'] ) && $_GET['orderby'] == 'price' ) { | |
// Modify the main query | |
if( ! is_admin() && $query->is_main_query() ) { | |
$args = array( | |
'relation' => 'AND', | |
'query_one' => array( | |
'key' => '_stock_status', | |
), | |
'query_two' => array( | |
'key' => '_price', | |
), | |
'orderby' => array( | |
'query_one' => 'ASC', | |
'query_two' => 'ASC', | |
)); | |
$query->set( 'meta_query', $args ); | |
$query->set( 'order', 'ASC' ); | |
} | |
} | |
} | |
add_action( 'pre_get_posts', 'theme_modify_the_main_query' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment