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

                                          $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(); ?>
  • What have you tried so far? What did you find when you searched for e.g. 'wordpress popular post for week and month' ? – mozboz Jul 25 at 13:03
  • 3rd party plugin support is offtopic here, you should ask at the plugins support routes – Tom J Nowell Jul 25 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 Jul 26 at 4:04

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.

| improve this answer | |

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.