显示最近7天的帖子

时间:2013-05-13 作者:Swen

我正试图在我的网站上显示上周(7天)5篇评分最高的帖子,但我似乎不知道如何显示它们。

以下是我迄今为止取得的成绩,但似乎并不奏效:

<?php $slider_query = new WP_Query(\'posts_per_page=5&cat=3&orderby=highest_rated&order=desc\'); ?>

<?php

$mylimit = 7 * 86400; //days * seconds per day

while ($slider_query->have_posts()) : $slider_query->the_post();

    $post_age = date(\'U\') - get_post_time(\'U\');

    if ($post_age < $mylimit) { 
?>

//The Post

<?php } ?>

<?php endwhile;?>

6 个回复
最合适的回答,由SO网友:Christine Cooper 整理而成

除了birgire的解决方案之外,从WordPress 3.7开始,您还可以使用Date parameters.

您的参数如下所示,用于筛选过去7天的帖子:

$args = array(
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'orderby\' => \'date\',
    \'order\' => \'DESC\',

    // Using the date_query to filter posts from last week
    \'date_query\' => array(
        array(
            \'after\' => \'1 week ago\'
        )
    )
); 

SO网友:birgire

我想这个问题在WordPress的答案上已经解决了很多次了。

您还可以查看examples in the Time parameters 食品法典中的一部分WP_Query.

这里有两个(根据您的需要稍作修改)

Example 1:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = \'\' ) {
    // posts in the last 7 days
    $where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'-7 days\')) . "\'";
    return $where;
}

add_filter( \'posts_where\', \'filter_where\' );
$slider_query = new WP_Query(\'posts_per_page=5&cat=3&orderby=highest_rated&order=desc\');    
remove_filter( \'posts_where\', \'filter_where\' );

Example 2:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = \'\' ) {
    // posts for May 1 to March 8, 2013
    $where .= " AND post_date >= \'2013-05-01\' AND post_date < \'2013-05-8\'";
    return $where;
}

add_filter( \'posts_where\', \'filter_where\' );
$slider_query = new WP_Query(\'posts_per_page=5&cat=3&orderby=highest_rated&order=desc\');
remove_filter( \'posts_where\', \'filter_where\' )
假设你有这个orderby=highest_rated 上面的评论中描述了一些插件。

SO网友:RRikesh

WP_Query Time Parameters 第节:

仅返回当前周的帖子:

$week = date(\'W\');
$year = date(\'Y\');
$query = new WP_Query( \'year=\' . $year . \'&w=\' . $week );

SO网友:Shameem Ali

像这样为我工作,显示过去7天的帖子,根据浏览量和按帖子排序的浏览量计数描述。

                $date_range = strtotime ( \'-7 day\' );  
                $args = array(
                    \'post_type\'         => \'post\',
                    \'post_status\'       => \'publish\',
                    \'posts_per_page\'    => \'10\',
                    \'meta_key\'          => \'post_views_count\',
                    \'orderby\'           => \'meta_value_num\',
                    \'order\'             => \'DESC\', 
                    \'date_query\'        => array(
                        array(
                            \'after\' => array(
                                \'year\'  => date(\'Y\', $date_range ),
                                \'month\' => date(\'m\', $date_range ),
                                \'day\'   => date(\'d\', $date_range ),
                            ),
                        )
                    )
                );


                $query = new WP_Query( $args );

SO网友:D.JCode

您可以简单地使用wp_get_archives()

 wp_get_archives( array( \'type\' => \'weekly\', \'limit\' => 1, \'show_post_count\' => \'true\' ,\'format\'=>\'option\') ); ?>

SO网友:Irfan

使用sql查询wordpress挂钩更简单posts where

function getStartAndEndDate($week, $year) {
$dto = new DateTime();
  $dto->setISODate($year, $week);
  $ret[\'week_start\'] = $dto->format(\'Y-m-d\');
  $dto->modify(\'+6 days\');
  $ret[\'week_end\'] = $dto->format(\'Y-m-d\');
  return $ret;
}

add_filter( \'posts_where\', \'wpse29897_no_parents\', 10, 2 );
function wpse29897_no_parents( $where, $query )
{
    if( isset( $query->query_vars[\'post_type\'] ) && \'menu_plans\' == $query->query_vars[\'post_type\'] )
    {
        if( \'\' != $where )
    {
        
        $currentdate = date(\'Y-m-d\');
        $currdate = new DateTime($currentdate);
        if($_GET[\'weekchk\']){
            $currentweek = $_GET[\'weekchk\'];
        }else{
            $currentweek = $currdate->format("W");
        }
        $week_array = getStartAndEndDate($currentweek,date(\'Y\'));
        $ws = "\'".$week_array[\'week_start\']."\'";
        $we = "\'".$week_array[\'week_end\']."\'";
        $where .= " AND post_date >= ".$ws." AND post_date < ".$we."";
    }
    else
    {
        $where .= " AND post_date >= \'2020-07-28\' AND post_date < \'2020-08-4\'";
    }
}
return $where;
}

结束

相关推荐

CREATE TABLE失败列日期DateType

我有一个创建表的插件,其中一个插件失败了,我觉得这是因为我的语法,或者是因为我试图创建一个数据类型为DATE而不是TIMESTAMP的列(只有一个我可以使用)。有人能看看我下面的语法并告诉我出了什么问题吗?$table = $wpdb->prefix.SSM_ACADEMIC_YEAR_TABLE; $sql = \"CREATE TABLE IF NOT EXISTS \" . $table . \" ( id med