Hello there, i wanted to show on a block the most popular posts among the posts of last 7 days.
Let me be more clear: Not posts-of-any-time that gain the most popularity the last 7 days.
I wanted posts-of-last-7-days that gain the most popularity.
So i implemented the “Popular among posts of last 7 days” sorting method. It took me 5 years in isolation, a team of nuclear physicists and billions of dollars to do it. And i give it now for free. How kind!
IMPORTANT REQUIREMENTS: ***YOU*** NEED TO KNOW PHP PROGRAMMING TO DO THIS. LINE NUMBERS OF CODE PROVIDED BELOW ARE NOT EXACT, THEY ARE JUST TO GIVE YOU A HINT WHERE TO LOOK AT, BECAUSE I DON’T KNOW WHAT VERSION ARE THESE FILES SO LINE NUMBERS MAY BE DIFFERENT AMONG DIFFERENT VERSIONS. SO DONT BLINDLY COPY PASTE THIS STUFF ON THAT LINES.
ONE MORE TIME WARNING: be carefull, you are messing with php code here, if anything goes wrong, i have nothing to do with it, this comes with no warranty at all do it at your own risk and its NOT OFFICIALLY SUPPORTED I DONT WORK FOR TAGDIV.
So:
ADD this case in the switch of Newspaper/includes/wp_booster/td_data_source.php its somewhere near (not exactly) 140-something line
Add:
case 'popular_of_latest':
$wp_query_args['meta_key'] = td_page_views::$post_view_counter_key;
$wp_query_args['orderby'] = 'meta_value_num';
$wp_query_args['order'] = 'DESC';
$wp_query_args['date_query'] = array(
'column' => 'post_date_gmt',
'after' => '1 week ago'
);
break;
ok now we need to add an array element in Newspaper/includes/td_config.php
find this block of code near line 4020-something and change this
array(
"param_name" => "sort",
"type" => "dropdown",
"value" => array (
'- Latest -' => '',
'Random posts Today' => 'random_today' ,
'Random posts from last 7 Day' => 'random_7_day' ,
'Alphabetical A -> Z' => 'alphabetical_order',
'Popular (all time)' => 'popular',
'Popular (jetpack + stats module requiered) Does not work with other settings/pagination' => 'jetpack_popular_2',
'Popular (last 7 days) - theme counter (enable from panel)' => 'popular7',
'Featured' => 'featured',
'Highest rated (reviews)' => 'review_high',
'Random Posts' => 'random_posts',
'Most Commented' => 'comment_count'
),
to this
array(
"param_name" => "sort",
"type" => "dropdown",
"value" => array (
'- Latest -' => '',
'Random posts Today' => 'random_today' ,
'Random posts from last 7 Day' => 'random_7_day' ,
'Alphabetical A -> Z' => 'alphabetical_order',
'Popular (all time)' => 'popular',
'Popular (jetpack + stats module requiered) Does not work with other settings/pagination' => 'jetpack_popular_2',
'Popular (last 7 days) - theme counter (enable from panel)' => 'popular7',
'Featured' => 'featured',
'Highest rated (reviews)' => 'review_high',
'Random Posts' => 'random_posts',
'Most Commented' => 'comment_count',
'Popular among posts of last 7 days' => 'popular_of_latest'
),
as you can see the only “change” was the addition of that last element, and adding a comma at the end of the previous element.
now this sorting option should be available on your blocks here and there

The kind good people at tagdiv may wanna add this at a future release as i find this useful and its just a quick commit.
p.s. the db query produced is fine performance-wise.
-
This topic was modified 10 years by
dimitris.
Hi,
Thank you for posting this solution. I’m sure it will help lots of people who are looking for such a sorting option.
We also appreciate the humor so..2 big THUMBS UP!!! I will add this solution as a feature request on our list for future updates.
Thank you!
Thank you dimitris! I needed this for Most Popular Posts of Last Year, so I changed ‘after’ => ‘1 week ago’ to ‘after’ => ‘1 year ago’.
Hi
Thank you for the code. It worked perfectly. I’m using WP Super Cache plugin, and when I turn Caching ON this new sorting stops working.
I tried enabling/disabling “7 days post sorting” in the Newspaper theme panel, but with no success. Seems like the Caching plugin prevents this code to work.
Is there any workaround I can use?
thank you
Hi,
You can maybe try to activate the ajax post views as if you use caching, the views themselves will be cached as well: http://screencast.com/t/eqB0ud0RM9
Thanks.
There is another option in the theme panel needed for the popular 7 filter: http://screencast.com/t/s2srmdtby
Also try to disable all plugins except visual composer, clear cache and reset cdn files to rule out a plugin conflict. Then check the views again.
Thanks.
Just noticed one thing- The number of popular posts that for last 7 days was selected to 6, but it’s currently showing only 4 posts on my home page. ( Have a look here: https://snag.gy/MNjc8i.jpg )Why is it so? Doesn every post published in last 7 days need to get views more than some fix post views number? I have published more than 10 posts in the last 7 days.
What can be causing this issue. Please help. Thank you.
Hi,
Well all of the ‘popular’ sorting options need views. If your post do not have any views after you activate the option, there will be nothing shown in the block. Here is a guide for the default popular 7 days sorting which specifies this: https://forum.tagdiv.com/7-days-post-sorting/
the counter starts after you activate the option. It will not count back 7 days. it will start at day 1 hour 1 and go from there.
Thanks
Hey,
Just one thing, will it start counting views from the time, I activated this option ? Because other settings like Ajax post views count under post setting is and “Use 7 days post sorting” under block setting is already ON for last 10 days.
Also, if it counts the view since it is enabled on the homepage, then how does it show even those 4 posts?
Does “activating this option” mean creating this option or activating it on the homepage?
Anyways, I will wait for it to work well.
Thank you very much for your insightful comment.
Hello,
The 7 days post count will start counting from the moment you activate this option: http://screencast.com/t/9ICXxQ7VY, and set it to one of your blocks. Posts will show up if they gain views. If you have it on from 10 days back, all your posts that got views will show up in a ‘most popular’ sorting order.
if you want to have the custom sorting option from this topic you need to create it and then set it to your blocks,. the count will start once you set the new option to a block.
Thank you!
Hey, thanks for this and I have a somewhat easier way of implementing an alternate sort, in my case sort posts by last MODIFIED date, not last PUBLISHED date. With my method you have to “sacrifice” one of the existing sorts and in my case I decided to replace the “Oldest posts” since I know I would not ever use that one.
It’s only two files you need to change, make sure you back them up and also understand that when you do a theme update these will be overwritten so you will want to keep copies. These apply to v. 8 of the theme.
First make the below changes to includes/td_config.php, starting at line 7351:
array(
“param_name” => “sort”,
“type” => “dropdown”,
“value” => array (
‘- Latest -‘ => ”,
// Sacrificing the ‘oldest posts’ sort, we’ll never use it
‘Modified date’ => ‘oldest_posts’,
Then make this change to includes/wp-booster/td_data_source.php, starting at line 116:
$wp_query_args[‘cat’] = get_cat_ID(TD_FEATURED_CAT); //add the featured cat
break;
case ‘oldest_posts’:
// Replaced by me w/sort by modified date, newest first
$wp_query_args[‘orderby’] = ‘modified’;
$wp_query_args[‘order’] = ‘DESC’;
break;
case ‘popular’:
That’s it. Now when you are doing a grid sort you will see Modified date instead of Oldest posts. This is sorting most recently modified first (‘DESC’) but if for some reason you wanted the oldest by modified date you could change it to ‘ASC’.
Also, in case anyone is not aware of this, there is a free plugin called “Last Modified Timestamp” that will add a Last Modified Date column to your Posts and Pages admin screens. For what we do as a newspaper, in most cases the last time o post was modified is much more important than the published date of a post as far as which posts to display in the grid(s).
Hope this helps somebody and thanks to everyone who posts solutions like these as many of them have helped me in the past.