Hello I have code intended to get the most viewed post for last 2 days and it seems like not working or maybe my code is just incorrect. I appreciate any help. Thanks.

  $args = array(
    'post_type' => 'post',
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
        'date_query' => array(
                'after'  => '2 days ago',

$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul class="posts-list">';
    while ( $the_query->have_posts() ) {
        $the_query->the_post(); ?>
        <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_post_thumbnail(); ?>
        <?php endif; ?>

        <div class="content">
            <time datetime="<?php echo get_the_date(DATE_W3C); ?>"><?php the_time('d, F Y') ?></time>
            <span class="comments">
                <a href="<?php comments_link(); ?>" class="comments"><i class="fa fa-comments-o"></i> <?php echo get_comments_number(); ?></a>          
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title();?></a>
    <?php }
    echo '</ul>';
    /* Restore original Post Data */
} else {
    // no posts found
  • What's not working - it's not getting the list of posts you expect, or the list is empty, or it's breaking the rest of the page? And just to clarify you're searching for the post that is dated within the last two days that has the most views, not a post from any time that's most popular counting only views in the last two days? – Rup Jul 21 '16 at 10:29
  • The list is empty, not returning any result. I am trying to list the most viewed post from the last 2 days like the trending for the last 2 days. Thank you for asking. – Mark Joey Lavapie Jul 21 '16 at 10:33
  • The date_query, by default, use the published date. So your query says: "get the posts published in the last 2 days and sort them by post_views_count meta key"; is it the logic you want? If so, I don't see anything wrong y your code. – cybmeta Jul 21 '16 at 10:51
  • @cybmeta no, my logic is to get the most viewed post for the last 2 days not to get the posts for the last 2 days and sort them for most views. – Mark Joey Lavapie Jul 21 '16 at 10:54
  • 1
    Can you explain how do you store the "post views count"? Are you storing "post views count" and associated dates? I think you are storing only "total post views count" in a meta field. With that data you can not do what you are trying to do. By the way, there are tons of similar questions in this site; try to search for them an see how others are doing it. – cybmeta Jul 21 '16 at 11:02

I had the same issue. I had searched many times for this issue. but I could not find a solution for this.

get the most viewed post for the last 2 days not to get the posts for the last 2 days

I have a table named "wp_popularpostssummary".

enter image description here

Result ll be

enter image description here

so from this table get result postids by using following custom query

$results = $wpdb->get_results($wpdb->prepare('SELECT postid,COUNT(*) AS qy FROM `wp_popularpostssummary` WHERE `view_date` BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK) AND CURRENT_DATE() GROUP BY postid ORDER BY qy DESC'));

query result ll be the post id's of most viewed posts for the last one week. then using following foreach loop to get post id

            $pids = [];

                $results = json_decode(json_encode($results), true);

                foreach ($results as $result) {
                   foreach ($result as $k => $v) {
                       if($k == 'postid'){
                           array_push($pids, $v);


finally, use post id to fetch post details. code ll be following

                $args = array(
                    'post_type'         => 'post',
                    'post_status'       => 'publish',
                    'posts_per_page'    => '10',
                    'orderby'           => 'post__in',
                    'post__in'          => $pids


                $query = new WP_Query( $args );   if ( $query->have_posts() ) {
                    // The Loop
                    while ( $query->have_posts() ) { //code here

Change date duration according to you needs. I hope this ll help to anyone in the feature!
Let me know if have a query.


Try adjusting your $args. You will need to get the date from two days ago and then pass the year, month and day into the after argument as an array.

Try this:

$date = strtotime ( '-2 day' ); // Date from two days ago

$args = array(
    'post_type' => 'post',
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'date_query' => array(
            'after' => array(
                'year'  => date('Y', $date ),
                'month' => date('m', $date ),
                'day'   => date('d', $date ),

More documentation about WP_Query and its arguments, including date_query can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.