I have posted this up in various forums, but no luck so far (hope its ok to post it here also?)…I have been trying for a few weeks…no success.
I have two pages on my blog (top rated and most viewed) which return a column of posts (with titles/thumbs), but at the top of these column’s I would like to “filter” these posts into week/month/all time. (Since I followed a tutorial, I wanted to keep it the same, so I will change the sorting options from date etc later).
The following code is mean refresh the page and load the new url like example.com/top-rated/?o=title-asc. But so far it displays the popular posts and the drop down menu with options…when I click nothing actually happens.
I have piggybacked my site on a friends domain, if anybody could take a look at let me know what you think…it would appreciated.
Thanks in advance folks 🙂
Here is my code:
<?php
/*
* Template Name: Top Rated
*/
get_header(); ?>
<?php
if( isset($_GET['o']) && $_GET['o'] != '')
{
$order = $_GET['o'];
switch($order)
{
case 'date-asc': $orderby = 'order=ASC';
$msg = 'Date Ascending';
break;
case 'date-desc': $orderby = 'order=DESC';
$msg = 'Date Descending(default)';
break;
case 'date-mod': $orderby = 'orderby=modified';
$msg = 'Date Modified';
break;
case 'title-asc': $orderby = 'orderby=title&order=ASC';
$msg = 'Title A-Z';
break;
case 'title-desc': $orderby = 'orderby=title&order=DESC';
$msg = 'Title Z-A';
break;
case 'comment': $orderby = 'orderby=comment_count';
$msg = 'Comment Count';
break;
}
}
else
{
$orderby = 'order=DESC';
$msg = 'Date Descending (default)';
}
?>
<div id="content">
<div id="sorter-container">
<script type="text/javascript">
var orderby = jQuery('#order-by');
var str;
orderby.change(function(){
str = jQuery(this).val();
window.location.href = "<?php echo home_url(); ?>/top-rated/?o="+str;
});
</script>
<h2 id="sort-heading">Posts ordered by:<?php echo $msg; ?></h2>
<select id="order-by">
<option value="date-desc" <?php echo (!isset($order) || $order == '' || $order == 'date-desc')? 'selected="selected"':''; ?>>Date Desc. (default)</option>
<option value="date-asc" <?php echo ($order == 'date-asc')? 'selected="selected"':''; ?>>Date Asc</option>
<option value="date-mod" <?php echo ($order == 'date-mod')? 'selected="selected"':''; ?>>Date Modified</option>
<option value="title-desc" <?php echo ($order == 'title-desc')? 'selected="selected"':''; ?>>Title Desc.</option>
<option value="title-asc" <?php echo ($order == 'title-asc')? 'selected="selected"':''; ?>>Title Asc.</option>
<option value="comment" <?php echo ($order == 'comment')? 'selected="selected"':''; ?>>Comments Count</option>
</select>
</div>
<?php query_posts('meta_key=votes_count&orderby=meta_value_num&order=DESC&posts_per_page=10'); ?>
<?php if ( have_posts() ){ ?>
<?php while ( have_posts() ) : the_post() ?>
<div <?php echo post_class(); ?>>
<h3> <a href="<?php the_permalink(); ?>"class="img_hover_trans"><?php the_post_thumbnail('featured-small'); ?></a>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> | <small><?php the_time('F jS, Y'); ?></small></h3>
<?php the_excerpt('Read more...'); ?>
</div>
<?php endwhile; ?>
<?php } ?>
</div>
<?php
get_sidebar();
get_footer();
Turns out I had some code in the wrong place (sorry, Im new to this)…so finally spotted it after Kemal posted up some code to try. So thanks for the help/support, I will update the OP with the new working code, it should work by copying and pasting into your project. If so, pay close attention to this part
window.location.href = "<?php echo home_url(); ?>/top-rated/?o="+str;replace top-rated with your page-slug. If anybody figures out how to sort by week/month/alltime, please post up here (As it would be helpful)`