Skip to content

Instantly share code, notes, and snippets.

@thetwopct
Created May 9, 2019 17:12
Show Gist options
  • Save thetwopct/e2aef4ca9c8eff4be2bb1286da8770f1 to your computer and use it in GitHub Desktop.
Save thetwopct/e2aef4ca9c8eff4be2bb1286da8770f1 to your computer and use it in GitHub Desktop.
Custom Orderby
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