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:
<?php
$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() ) {
$A->the_post();
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()) {
$B->the_post();
echo '<li>' .get_the_title() .'</li>';
}
//Restoring original post data
wp_reset_postdata();
?>
<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.
?orderby=title
in the URL and it will 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 =/