0

I would like to know how can I make my widget get the popular post by week ? Right now it show only popular post since the beginning of the website.

here is my code :

// PopularPosts Widget

{
function PopularPosts(){
$widget_ops = array('description' => 'Shows the most commented posts.');
$control_ops = array('width' => 200, 'height' => 300);
parent::__construct(false,$name='[Voxis] Articles populaires',$widget_ops,$control_ops);
}

/* Displays the Widget in the front-end */

function widget($args, $instance){
extract($args);

$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
$number_posts = $instance['number_posts'];
$categories = $instance['categories'];

echo $before_widget; 

if ( $title != '' )
    echo $before_title . $title . $after_title;

echo '
<div class="list">
    <ul>';
$args = array();
$args['posts_per_page'] = $number_posts;
$args['orderby'] = 'comment_count';
$args['order'] = 'DESC';
if(!empty($categories) )
    $args['category__in'] = $categories;
$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();
echo '</ul>
</div>';

echo $after_widget;}

/*Saves the settings. */

function update($new_instance, $old_instance){
$instance =  array();
$instance['title'] = esc_attr($new_instance['title']);
$instance['number_posts'] = (int)$new_instance['number_posts'];
$instance['categories'] = $new_instance['categories'];

return $instance;}

/*Creates the form for the widget in the back-end. */

function form($instance){
//Defaults
$instance = wp_parse_args( (array) $instance, array('number_posts'=> 5, 'title' => '', 'categories' => array() ) );

$title = esc_attr($instance['title']);
$number_posts = (int) $instance['number_posts'];
$categories = (array) $instance['categories'];

echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
echo '<p><label for="' . $this->get_field_id('number_posts') . '">' . 'Number of posts: ' . '</label><input id="' . $this->get_field_id('number_posts') . '" name="' . $this->get_field_name('number_posts') . '" value="'. esc_textarea($number_posts)  . '" /></p>';
$cats = get_categories('hide_empty=0');
?>
<p>
    <label>Categories to include: </label> <br />
    <?php foreach( $cats as $category ) { ?>
        <label>
            <input type="checkbox" name="<?php echo $this->get_field_name('categories'); ?>[]" value="<?php echo $category->cat_ID; ?>"  <?php if(in_array( $category->cat_ID, $categories ) ) echo 'checked="checked" '; ?> /> 
            <?php echo $category->cat_name; ?>
        </label> <br />
    <?php } ?>
</p> 
<?php } }

// end PopularPosts class

Thank you all for the Support...

  • Do order by date and comment count – Shady M Rasmy Jan 25 '15 at 23:11
  • look into this plugin: wordpress.org/plugins/wordpress-popular-posts – Tara Jan 25 '15 at 23:53
  • I don't understand how your widget works currently, it's just listing posts, it doesn't involve popularity at all. Also there is unformatted code, can you edit your post and make sure all code is interpreted as code so it's clearer? – Tom J Nowell Jan 25 '15 at 23:55
  • Thanks Shady i already use it and i m trying avoid it and use personal coded one this code above is fine just cant make it work by week! – Dolomats Jan 25 '15 at 23:56
  • Also how are you tracking popularity? What makes a post popular, page views? – Tom J Nowell Jan 25 '15 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.