admin管理员组

文章数量:1331616

I have a code and can not use week or month filter for it. Please check the code and tell me how can I display popular post by week or months

                <?php
                                          $popularpost = new WP_Query(array(
                                          'posts_per_page' => $cat,
                                          'meta_key' => 'post_views_count',
                                          'orderby' => 'meta_value_num',
                                          'order' => 'DESC',
                                          ));
                                         while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>

I have a code and can not use week or month filter for it. Please check the code and tell me how can I display popular post by week or months

                <?php
                                          $popularpost = new WP_Query(array(
                                          'posts_per_page' => $cat,
                                          'meta_key' => 'post_views_count',
                                          'orderby' => 'meta_value_num',
                                          'order' => 'DESC',
                                          ));
                                         while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
Share Improve this question asked Jul 25, 2020 at 12:42 m.javad koushkim.javad koushki 54 bronze badges 3
  • What have you tried so far? What did you find when you searched for e.g. 'wordpress popular post for week and month' ? – mozboz Commented Jul 25, 2020 at 13:03
  • 3rd party plugin support is offtopic here, you should ask at the plugins support routes – Tom J Nowell Commented Jul 25, 2020 at 13:05
  • I want to do that without any plugin and with this code I just can display popular posts for all time not by week or month. – m.javad koushki Commented Jul 26, 2020 at 4:04
Add a comment  | 

1 Answer 1

Reset to default 0

Unless you're storing that data, you can't.

It looks like posts_views_count is just a running total, so unless you've stored per month/week data, it simply does not exist.

What's more, you're storing that data in a post meta value, which has a lot of performance and accuracy problems. It's:

  • incompatible with any form of page caching or CDN
  • creates write queries on the frontend, slowing down your site and reducing how many people can visit at the same time
  • extremely innacurate due to race conditions. The view counts will be lower than they actually are

If you want to do it properly, and have per month/week data, you'll need to use an external service such as Google Analytics, or a dedicated traffic software solution.

Otherwise, to make the plugin track per week and month, you will need to contact their support routes. 3rd party plugin support is offtopic here. If it does store that data, then you'll need to contact their support routes to ask how to access it.

本文标签: popular post for week and month