Skip to content

Instantly share code, notes, and snippets.

@gecugamo
Created November 30, 2015 18:12
Show Gist options
  • Save gecugamo/9d15e875f9ee7ecfeb38 to your computer and use it in GitHub Desktop.
Save gecugamo/9d15e875f9ee7ecfeb38 to your computer and use it in GitHub Desktop.
<?php
function products_query( $query )
{
if ( ! $query->is_main_query() || is_admin() )
{
return $query;
}
if ( is_post_type_archive( 'product' ) || is_tax())
{
if (!isset($_GET['sort']) || empty($_GET['sort'])) {
return;
}
// if sort a-z
if ( $_GET['sort'] === 'a-z' )
{
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}
// if sort z-a
if ( $_GET['sort'] === 'z-a' )
{
$query->set('orderby', 'title');
$query->set('order', 'DESC');
}
// if sort lastest
if ( $_GET['sort'] === 'latest' )
{
$query->set('orderby', 'date');
$query->set('order', 'DESC');
}
}
}
add_action('pre_get_posts', 'tg_products_query');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment