9

I'm trying to display the 5 best rated posts of the last week (7 days) on my website, however I can't seem to figure out how to display them.

Here's what I've achieved so far but it doesn't seem to work:

<?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;?>
2
  • Note that orderby=highest_rated isn't inbuilt in WordPress. Are you using some plugin for ratings?
    – RRikesh
    Commented May 13, 2013 at 13:07
  • Yes I am using a plugin for this :) Thank you for your concern!
    – Swen
    Commented May 13, 2013 at 13:25

6 Answers 6

27

In addition to birgire's solution, as of WordPress 3.7, you can use Date parameters.

Your arguments would look like this to filter posts from the last 7 days:

$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'
        )
    )
); 
2
  • 5
    This should be the approach as from 3.7 :-) Commented Nov 27, 2014 at 15:41
  • This is good but it gets posts exactly 24*7 hours ago. In example, if it currently is night time, it won't retrieve posts published 7 days ago in the morning. Commented Mar 3, 2023 at 5:23
6

I think this must have been solved many times here on WordPress Answers.

You could also check out the examples in the Time parameters part in Codex for WP_Query.

Here are two of them (slightly modified to your needs)

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

assuming that you have this orderby=highest_rated covered with some plugin as you describe in the comment above.

2
  • 1
    I'm wondering whether if 'date_query' is better to use? By adding the following to the wp_query arguments: 'date_query' => array( array('after' => '1 week ago')
    – Christine Cooper
    Commented Nov 26, 2014 at 16:12
  • 3
    Yes, that would now be the preferred method, since the answer was written prior to the existence of the date_query parameter ;-) @ChristineCooper
    – birgire
    Commented Nov 27, 2014 at 16:13
0

From the WP_Query Time Parameters section:

Returns posts for just the current week:

$week = date('W');
$year = date('Y');
$query = new WP_Query( 'year=' . $year . '&w=' . $week );
1
  • 2
    Hey. I haven't tested this yet, but I'm pretty sure it's not what I'm looking for exactly. This probably lists posts posted in week 13 for example. What I want is the posts that were posted in the past 7 days. So for example, from Wednesday 8 May until Wednesday 1 May.
    – Swen
    Commented May 13, 2013 at 13:27
0

Working for me like this, to show post from last 7 days according to the number of views and order by post views count DESC.

                $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 );
0

you can simply use wp_get_archives()

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

More Simple Using sql query wordpress hook 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;
}

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.