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="https://wordpress.stackexchange.com/questions/232809/<?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="https://wordpress.stackexchange.com/questions/232809/<?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
}

2 Answers
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *