Display popular posts by how many hits it got within 30 days of publishing

As Peter mentioned, Jetpack Stats would be a neat way to handle this. It has its own Popular Posts widget which you can take as a starting point, the API is fairly simple.

An alternative is the Google Analytics API. I wrote a tutorial about this quite a while ago, it’s probably somewhat outdated, but it can be a good reference at what you should be looking for: http://theme.fm/2011/10/using-the-google-analytics-api-in-wordpress-2644/

Both approaches will allow you to limit the stats data to a certain date range.

And since you asked for “how many hits they received within the first 30 days of being published” I’ll try and give some further thoughts.

I like that you’re looking at the first 30 days and not the last 30 days. It’ll give you a number that’s probably much less affected by search engine rankings, etc. It’s also a static number that once obtained, will never change, which means you can “cache” it forever.

But since each post can have a different publish date, it’ll be pretty difficult to query stats for multiple posts at once, you’ll most likely have to query them one by one.

I would run a background task that would look for posts older than 30 days, and ones that do not have our value cached yet. I would pick the first 50-100 such posts and query a stats API (Jetpack or Google Analytics) for each for the {published_date}-{published_date + 30 days} range and store the number of pageviews.

Running this task every hour or so will eventually fill out all your old posts, and you can then adjust the schedule to best suit your publishing intensity. And once you have this data, you can use it when querying your “popular” posts.

techhipbettruvabetnorabahisbahis forumutaraftarium24edusedusedusedusedusedusedusedusedusedu