按周/月/所有时间对帖子进行排序

时间:2012-09-27 10:38:50

标签: php wordpress sorting filter

所以我设法用一些基本设置对帖子进行排序。我想从date-asc,date-dec,comments等周,月,所有时间改变它。我也希望加入以下代码,这些代码只会排序最高投票的帖子,而不是所有帖子

meta_key=votes_count&orderby=meta_value_num&order=DESC&posts_per_page=10

我目前评价最高的页面,对我的日期 - 标题,标题等进行排序:

<?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">


<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>
    <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>

EDIT2: 好的,所以这就是我所拥有的,它不再有效,所以我只是继续并将周/月/时间添加到我认为它应该去的地方(我知道它不正确,但我不确定如何将排序限制为仅7天等。

<?php
if( isset($_GET['o']) && $_GET['o'] != '')
      {
          $order = $_GET['o'];
          switch($order)
          {
            case 'week': $orderby = 'meta_key=votes_count&orderby=meta_value_num&order=DESC&WEEK&posts_per_page=10';
                        $msg = 'This Week (default)';
                          break;

            case 'month': $orderby = 'meta_key=votes_count&orderby=meta_value_num&order=DESC&MONTH&posts_per_page=10';
                            $msg = 'This Month';
                           break;

            case 'alltime': $orderby = 'meta_key=votes_count&orderby=meta_value_num&order=DESC&ALLTIME&posts_per_page=10';
                            $msg = 'All Time';
                            break;
      else
      {
          $orderby = 'order=DESC';
          $msg = 'This Week (default)';
      }
?>

<div id="content">

<div id="sorter-container">


<h2 id="sort-heading">Posts ordered by:<?php echo $msg; ?></h2>
    <select id="order-by">
        <option value="week" <?php echo (!isset($order) || $order == '' || $order == 'week')? 'selected="selected"':''; ?>>This Week (default)</option>
        <option value="month" <?php echo ($order == 'month')? 'selected="selected"':''; ?>>This Month</option>
        <option value="alltime" <?php echo ($order == 'alltime')? 'selected="selected"':''; ?>>All Time</option>
    </select>
</div>

1 个答案:

答案 0 :(得分:0)

如问题评论中所述,您当前的代码库无法实现您想要的功能。

wp-postratings可以在常见问题中完全按照您的要求进行操作在给定时间段内显示评分最高的帖子
看看你应该能够解决这个问题。