Front End Sorting Questions

orderby defaults to date, so we can always leave that out if we want to order by date. so the only thing we need to detect is if $_GET['sort'] is set and it’s equal to title.

here we set the query args for all queries, and just add on the orderby if it’s title:

$args = array(
    'category_name' => get_queried_object()->post_name,
    'order'         => 'DESC'
);

if( isset( $_GET['sort'] ) && "title" == $_GET['sort'] ){
    $args['orderby'] = 'title';
}

$q = new WP_Query( $args );

Leave a Comment