Most viewed post for the last 2 days using WP_Query

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(
            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(); ?
        li
        ?php if ( has_post_thumbnail() ) : ?
            a href="?php the_permalink(); ?" title="?php the_title(); ?"
                ?php the_post_thumbnail(); ?
            /a
        ?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          
            /span
            a href="?php the_permalink(); ?" title="?php the_title(); ?"?php the_title();?/a
        /div
        /li
    ?php }
    echo '/ul';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}

Topic date-query wp-query posts Wordpress

Category Web


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.