1

I've try several different loop to display the most viewed post from the last 30 days . . However its just showing the post from 30 dasy ago . . not from the last 30 days . . not the most viewed . . iF anybody can help me out . .it will be fantastic, thank you =)

Here is my loop at the moment:

<ul>
<?php
function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
}
add_filter('posts_where', 'filter_where');

query_posts('post_type=post&posts_per_page=3&orderby=post_views_count&order=ASC');

while (have_posts()): the_post(); ?>

<li><div class="tabber-image">

                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('small-thumb'); ?></a>

                    </div>

                    <div class="tabber-text">
                        <?php 
                        //$meta = get_post_meta(get_the_ID());
                        //$short_title = $meta['maxmag_featured_headline'][0];
                        ?>

                        <a href="<?php the_permalink() ?>" class="main-headline"><?php the_title(); ?></a></li>

<?php
endwhile;
wp_reset_query();
?>
</ul>

ANy help would be amazing . .thank you for your time

EDIT ---

Last LOOP:

<ul>
<?php while (have_posts()) : the_post(); 

    $args = array(

    'post_type' => 'post',
    'posts_per_page' => 3,
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',

    'order'=> 'ASC',
    'date_query' => array(
        array(
            'after'  => '30 days ago'
        ),
    ),
);

$query = new WP_Query( $args );

     ?>

<li>

<div class="tabber-image">

                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('small-thumb'); ?></a>

                    </div>

                    <div class="tabber-text">
                        <?php 
                        //$meta = get_post_meta(get_the_ID());
                        //$short_title = $meta['maxmag_featured_headline'][0];
                        ?>

                        <a href="<?php the_permalink() ?>" class="main-headline"><?php the_title(); ?></a>

                        </li>

<?php endwhile; ?>
</ul>

ORIGINAL CODE FROM PLUGIN:

<div id="tab2" class="tabber-content">
            <?php $popular_posts = new WP_Query('showposts=' . $popular_number . '&orderby=comment_count&order=DESC'); if($popular_posts->have_posts()): ?>
            <ul>
                <?php while($popular_posts->have_posts()): $popular_posts->the_post(); ?>
                <li>
                    <div class="tabber-image">
                        <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('small-thumb'); ?></a>
                        <?php } ?>
                    </div>
                    <div class="tabber-text">
                        <a href="<?php the_permalink() ?>" class="main-headline"><?php the_title(); ?></a>
                        <p><?php echo excerpt(10); ?></p>
                        <div class="headlines-info">
                            <ul class="headlines-info">
                                <li>Posted <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></li>
                                <li class="comments-icon"><a href="<?php comments_link(); ?>"><?php comments_number( '0', '1', '%' ); ?></a></li>
                            </ul>
                        </div><!--headlines-info-->
                    </div>
                </li>
                <?php endwhile; ?>
            </ul>
            <?php endif; ?>
        </div><!--tab2-->
  • Any reason why you didn't use the date_query and it's after parameter as described here? codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters – Saurabh Shukla Mar 2 '15 at 12:25
  • What do you mean ? I didnt understand you sorry – tibewww Mar 2 '15 at 12:28
  • You have filtered the where query (for all queries) and then run query_posts that is not recommended by any one. Any specific reason why you had to do things in this roundabout manner than using an instance of WP_Query which supports this kind of queries (from the last 30 days, etc) as you can see on the codex page I've posted? – Saurabh Shukla Mar 2 '15 at 12:34
  • Because I have a straightforward answer to your question but if you are forced to do it this way, it'd be useless for you. – Saurabh Shukla Mar 2 '15 at 12:36
  • Hi .. hm im not sur to understand, does thsi looks better ? This one is not working also ) I jsut add in the edit of my post) just displaying the current month post and not last 30 days :(. . . my query skills are quite low if you have any highlite .. . would be wonderfull, Thank you for your time – tibewww Mar 2 '15 at 12:50
1

Try this:

$args = array(

    'post_type' => 'post',
    'posts_per_page' => 3,
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',

    'order'=> 'ASC',
    'date_query' => array(
        array(
            'after'  => '30 days ago'
        ),
    ),
);

$query = new WP_Query( $args );

You can't directly order by met_key's name, you need to use it the way it is above.

  • Ive edit the edit loop from my post . . I tried this one that you just recomended but it's not working correctly .. just showing one post from today . . . :( – tibewww Mar 2 '15 at 13:03
  • I forgot to change the -30 days to 30 days ago. Try now. – Saurabh Shukla Mar 2 '15 at 13:13
  • :( Thank you for your time . .but it still display only one post :( – tibewww Mar 2 '15 at 13:23
  • Oh, but I've only give you the query results. Did you loop through them? using while($query->have_posts()):the_post(); and endwhile; Otherwise you'll only display one. – Saurabh Shukla Mar 2 '15 at 13:30
  • Thank you, I ve edit my last edit code, But this is displaying much more post ( Around 20, and by ascendant date, its not taking count of the post_view_count :( – tibewww Mar 2 '15 at 13:45

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.