I am trying to add a front end sorting to the posts by default in most recent and adding two link options 1: Sort by Title 2: Sort by Date. However, my problem is that for me it's not working. I tried searching for answers on here and on Google and found this very similar fix which i am trying to follow but it isn't working for me. I have been trying for a long time but it just doesn't want to work for me.

Please see the code that I'm working with:

$args= [
'category_name' => get_queried_object()->post_name,
'order'         => 'DESC',
'orderby'       => 'date'

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

$A = new WP_Query( $args );

while ( $A->have_posts() ) {
echo '<li>' .get_the_title() .'</li>';

wp_reset_postdata(); /*instead of wp_reset_query?*/

/* 2nd Query Sort? */
$args2= [
'category_name' => 'Uncategorized',
'order'         => 'ASC',
'orderby'       => 'title'

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

$B = new WP_Query($args2);

// Loop 2?
while( $B->have_posts()) {
echo '<li>' .get_the_title() .'</li>';

//Restoring original post data

<a href="?sort=title">Sort By Title</a>
<a href="?sort=date">Sort By Date</a>

<?php query_posts($order.'&order=DESC');  ?>

I am trying to add 'Sort by Title' link in ASC order and 'Sort by Date' in DESC order as in Most Recent order.

Any help on this issue would be greatly appreciated.

  • If you're dealing with the main query then you can just put ?orderby=title in the URL and it will work. Commented Jan 31, 2018 at 14:56
  • Thank you for your answer but sorry it didn't work: <a href="?orderby=title">Sort By Title</a> Is there a way to get this to work please? I have been onit for hours trying to fix it =/
    – pv619
    Commented Jan 31, 2018 at 15:08

1 Answer 1


Thank you, Jacob your suggestion put me into the right direction.

This resolved the issue:

<a href="?orderby=title&order=ASC">Sort By Title</a>
<a href="?orderby=date&order=DESC">Sort By Date</a>

How can I upvote or add +rep you on here? thanks again bro :)

  • why -1 me for? that's a legit answer thought might help out someone.
    – pv619
    Commented Jan 31, 2018 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.