WordPress : Most populars posts of the Week or Month

I’m using a theme that shows popular posts with the code below, and I need
someone to help me edit this code to make it show popular posts on the last week or
month.

<?php 
                $args = array();
                $args['posts_per_page'] = $number_posts;
                $args['orderby'] = 'comment_count';
                $args['order'] = 'DESC';
                $query = new WP_Query($args);
                while($query->have_posts() ) : $query->the_post(); global $post; ?>
                    <li>
                        <a title="<?php the_title();?>" href="<?php the_permalink();?>">
                            <figure>
                                <?php if(has_post_thumbnail() ) { ?>
                                    <a href="<?php the_permalink();?>">
                                        <img src="<?php echo aq_resize(wp_get_attachment_url( get_post_thumbnail_id($post->ID) ), 42, 42, true); ?>" alt="<?php the_title();?>" />
                                    </a>
                                <?php } else { ?>
                                    <a href="<?php the_permalink();?>">
                                        <img src="<?php echo get_template_directory_uri() . '/img/missing_56.png';?>" alt="<?php the_title();?>" />
                                    </a>
                                <?php } ?>
                            </figure>
                            <p>
                                <a href="<?php the_permalink();?>"><?php the_title();?></a> <br /> 
                                <span> <?php _e('Le ', 'Voxis'); the_time("F d, Y");?>, <?php comments_popup_link(esc_html__('0 commentaires','Voxis'), esc_html__('1 commentaire','Voxis'), '% '.esc_html__('commentaires','Voxis')); ?> </span>
                            </p>
                        </a>
                    </li>
                <?php endwhile; wp_reset_postdata(); ?>
            </ul>

Related posts

Leave a Reply

2 comments

  1. I have not yet tested below code but will assure that this will work for you.

    <?php
    $weekday=date("d m Y",strtotime('-7days'));
    while($query->have_posts() ) : $query->the_post(); global $post; 
    
    $currentdate=date('d m Y',strtotime($post->post_date));
    if($weekday < $currentdate) { ?>
        <li>
            ...
            body of code in <li> tag
            ...
        </li>
    <?php } ?>
    ...
    rest of the code
    ?>
    
  2. replace your code to this(get most recent last month post)
    
     <?php 
                    $today = getdate();
                        $args=array(
                              'post_type' => 'post',
                              'year'=>$today["year"],
                              'monthnum'=>$today["mon"]-1,
                              'orderby'=>'comment_count',
                              'order'=>'DESC',
                              'posts_per_page' =>$number_posts
                        );
    
                        $query = new WP_Query($args);
                        while($query->have_posts() ) : $query->the_post(); global $post; ?>
                            <li>
                                <a title="<?php the_title();?>" href="<?php the_permalink();?>">
                                    <figure>
                                        <?php if(has_post_thumbnail() ) { ?>
                                            <a href="<?php the_permalink();?>">
                                                <img src="<?php echo aq_resize(wp_get_attachment_url( get_post_thumbnail_id($post->ID) ), 42, 42, true); ?>" alt="<?php the_title();?>" />
                                            </a>
                                        <?php } else { ?>
                                            <a href="<?php the_permalink();?>">
                                                <img src="<?php echo get_template_directory_uri() . '/img/missing_56.png';?>" alt="<?php the_title();?>" />
                                            </a>
                                        <?php } ?>
                                    </figure>
                                    <p>
                                        <a href="<?php the_permalink();?>"><?php the_title();?></a> <br /> 
                                        <span> <?php _e('Le ', 'Voxis'); the_time("F d, Y");?>, <?php comments_popup_link(esc_html__('0 commentaires','Voxis'), esc_html__('1 commentaire','Voxis'), '% '.esc_html__('commentaires','Voxis')); ?> </span>
                                    </p>
                                </a>
                            </li>
                        <?php endwhile; wp_reset_postdata(); ?>
                    </ul>
    
    ____________________________________________________________________________
    for last 7 days post use this code:
    
    <?php 
    function filter_where($where = '') {
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
        return $where;
    }
    add_filter('posts_where', 'filter_where');
    query_posts('post_type=post&posts_per_page='.$number_posts.'&orderby=comment_count&order=DESC');
    ?>
    
    <?php while (have_posts() ) : the_post(); ?>
    
                        <li>
                            <a title="<?php the_title();?>" href="<?php the_permalink();?>">
                                <figure>
                                    <?php if(has_post_thumbnail() ) { ?>
                                        <a href="<?php the_permalink();?>">
                                            <img src="<?php echo aq_resize(wp_get_attachment_url( get_post_thumbnail_id($post->ID) ), 42, 42, true); ?>" alt="<?php the_title();?>" />
                                        </a>
                                    <?php } else { ?>
                                        <a href="<?php the_permalink();?>">
                                            <img src="<?php echo get_template_directory_uri() . '/img/missing_56.png';?>" alt="<?php the_title();?>" />
                                        </a>
                                    <?php } ?>
                                </figure>
                                <p>
                                    <a href="<?php the_permalink();?>"><?php the_title();?></a> <br /> 
                                    <span> <?php _e('Le ', 'Voxis'); the_time("F d, Y");?>, <?php comments_popup_link(esc_html__('0 commentaires','Voxis'), esc_html__('1 commentaire','Voxis'), '% '.esc_html__('commentaires','Voxis')); ?> </span>
                                </p>
                            </a>
                        </li>
    
    
    <?php endwhile; 
            wp_reset_query();
    ?>