1

i want to display popular posts for 1 week ago and 1 month ago.these are codes that i used but these don't work correctly for 1 week ago

<ul>

    <?php
    $week = date('W');
    $year = date('Y');
    query_posts('meta_key=post_views_count&cat='.$link1.'&posts_per_page=9&orderby=meta_value_num&order=DESC&year=' . $year . '&weeknum=' . $week);

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

    <li>
    <h2><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></h2>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
    <?php
    endwhile;
    wp_reset_query();
    ?>

    </ul>

for 1 month ago

 <ul>

    <?php
    $month = date('m');
    $year = date('Y');
    query_posts('meta_key=post_views_count&cat='.$link1.'&posts_per_page=9&orderby=meta_value_num&order=DESC&year=' . $year . '&monthnum=' . $month);

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

       <li>
    <h2><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></h2>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
    <?php
    endwhile;
    wp_reset_query();
    ?>

    </ul>
  • 1
    You don't need to use week and year, this complicates things. Use 1 week ago or 1 month ago. – prosti Jan 1 '17 at 21:42
1

These $args is what you need and you may also use that in query_posts();

$args = array(
        'post_type'         => array( 'post' ),
        'post_status'       => 'publish',
        'posts_per_page'    => 9,
        'cat'               => $link1, // try better variable name

        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',          

        'date_query' => array(
        array(
          'after' => '1 months ago',
        ),
        )   
);

$r = null;
$r = new WP_Query($args);
// do something with $r = result
wp_reset_postdata();

I provided you some other way than query_posts(). BTW, I like you used wp_reset_query(); at the end.

| improve this answer | |
  • it doesn't work for me. – m.javad Koushki Jan 2 '17 at 6:32
  • Do you have post_views_count meta at all? Where you picked this code originally? – prosti Jan 2 '17 at 8:06
  • 1
    Try to comment meta_key, orderby, and order if you don't have this meta. Also, you may try all categories if you comment cat. You have many options for categories, such as category__in ... codex.wordpress.org/Class_Reference/WP_Query – prosti Jan 2 '17 at 11:50

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.