Query Problem in getting top viewed posts

I wanted to display top viewed posts by month, I add this code snip, to form a query and get required 15 posts, but it displays 3-4 posts.Here is my query:

global $post;

$args = array(
    'meta_key' => post_views_count,
    'orderby' => meta_value_num,
    'numberposts' => 15,
    'order' => 'DESC',
);

$top_viewed_posts = get_posts( $args );

foreach( $top_viewed_posts as $post ) :
    $mylimit= 30 * 86400;        //To display post for last 30 days 
    $post_age = date('U') - get_post_time('U');
    if ($post_age < $mylimit) {

        if (has_post_thumbnail()) { ?>
            <li>
                <div class="widgetimg">
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                </div>
                <span class="sidebarviews">Views: <?php echo getPostViews(get_the_ID());?></span>
            </li>
        <?php
        }
    }
endforeach; ?>

But when I remove this portion :

Read More
$mylimit= 30 * 86400;        //To display post for last 30 days 
$post_age = date('U') - get_post_time('U');
if ($post_age < $mylimit) {

It displays correctly 15 posts.

Please suggest!

Related posts

Leave a Reply