How to make an RSS feed to show most viewed posts for last 24 hours?

Your problem has 2 parts:

  • Q1: How do I retrieve view counts for a post for the last 24 hours and store them as post Meta
  • Q2: I’m storing view counts in post meta, how do I order an RSS feed by these counts?

Q1: How do I retrieve view counts for a post for the last 24 hours and store them as post Meta?

Using the google API library ofcourse! The google API however is out of scope for this site, this is a WordPress stack, not a Google one.

The other part of this piece of the puzzle, is that you’ll need to write a wp cron job, to find posts that have out of date data, and retrieve new view counts. None of this will be realtime, and you’ll incurr a heavy performance cost if you try to do it in realtime.

So store the view count as a post meta on a post, and store a second post meta with the time/date of when the post was last checked. Use this to power a WP_Query to fetch the out of date posts by looking for dates longer than a day or 12 hours ago. Do them in batches of say 10-15 so that your cron job doesn’t get killed. Also do 10-15 posts that don’t have either meta keys on, so that your existing content is checked

Q2: I’m storing view counts in post meta, how do I order an RSS feed by these counts?

Using query parameters! This is the easiest part, simply modify what the feed fetches by passing additional parameters ( see the WP_Query documentation for a full list of query parameters )

examples.com/feed/rss?orderby=meta_value&order=DESC&meta_key=your-postmetakeygoes-here

You can take those query parameters and put them on the end of other URLs too, such as a category archive, giving you the most viewed posts in that category as a feed, e.g.

example.com/category/kittens/feed/?orderby=meta_value&order=DESC&meta_key=your-postmetakeygoes-here

Note the meta_key parameter, you need to put the name of the post meta that you’re storing your view counts in here