0

I need to submit my site in somewhere and they're asking for a link to the rss of my most viewed posts during last day. But WordPress by default doesn't support such an option. Is there any way to make an RSS feed for the most visited posts of my blog in the last day?

  • How are you tracking which posts are most viewed? It'll be necessary to know how you're storing that information or if you're using a service to figure that out – Tom J Nowell Nov 26 '15 at 18:33
  • It's exactly my problem... I found some plugins that can show those posts in a widget. But they don't generate any rss feeds. Like this: wordpress.org/plugins/wordpress-popular-posts – Milad Nasri Nov 26 '15 at 20:23
  • So you're not currently tracking which posts are the most viewed? Ignoring entirely how they're shown, I'm asking if the data exists, and where it exists. E.g. on my website I have Google Analytics tracking views. – Tom J Nowell Nov 26 '15 at 23:34
  • I have the data... I have "Google Analytics" and "WordPress Popular Posts plugin"... Both of them track the post views but I don't know how to use them to create an rss feed for the most viewed post of the last 24 hours. I wish at least there was a plugin for that! – Milad Nasri Nov 27 '15 at 10:27
  • So you need to grab data from Google Analytics and store it in WordPress, then present posts ordered by that data, in your case an RSS feed. The Google Analytics piece of information is a critical part of your question, you should add it in. Can I ask who is asking for the RSS feed? This is the first time I've ever heard of such a request, it seems a little bizarre. Also note that answers on this site are developer answers, nobody will give you a plugin that already exists, and if you ask for one your question would be out of scope for this site – Tom J Nowell Nov 27 '15 at 13:14
1

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

| 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.