1

I've been looking for something that would automate the creation of a popular posts of the week blog post. So for instance, every Monday create post that is a list of the most popular posts from the last 7 days. And it would probably need to be able to limit the time scope, so say it was an old post that was getting the most traffic, I'd only want to highlight posts that were published in the last 7 days.

Now in looking for something like this as a plugin it seems I can only find widget plugins that do this kind of functionality. Is there a tool out there that might do this? Or would I need to create a function that would do something like this? Most of the code I've seen was about creating a dynamic page that would feature all time content, versus what I'm looking to do, which is just get a snapshot of the most popular content from a given week.

1

Is your goal to create a new post with the recent content or to list the existing posts that have the recent content? In general, the latter is more suited to what WP normally does.

Creating New Posts

This can be done but you're asking for some complexity. Assuming that you could just stick together the content from multiple posts, you'd be creating new content that contains copies of old content.

The assumption, that you could just stick together posts, may be difficult. Think about the metadata. Say you have post A in category Green and post B in category Red, into what category would you put post A+B, Green or Red? There may be content issues as well. You may also not be using categories (and your tags may be distinct) so this may not be an issue for you.

When you created A+B, you'd be creating this: "A+B as A existed at a particular time and as B existed at that time." Managing changes to content could become difficult. You're also creating a new post that doesn't have comments from either A or B. Once again, this may not be of issue to you for the setup you have. You mentioned snapshots so you likely already thought this through.

If this is what you want to do, you'd probably need to write something to deal with this the way you want it. That having been said, WP has been around for a long time and some research could find a plugin that does something similar. It may even be similar enough that you would be comfortable with the results. Otherwise, this is not a complicated thing to write, and best practice would probably be to wrap it in a plugin.

Listing Existing Posts

This is probably the more, "WordPress way," of doing this. This is done frequently for newsletters and feeds (like RSS). Some good ways of doing this:

  • A newsletter plugin that has a shortcode would probably handle this directly for you. Put the shortcode on the page where you want to use the content (or maybe you're just trying to write a newsletter).
  • Themes sometimes have this built in. The default WP front page is similar to this. Splitting off a child theme and adding a page template based off archive.php with a filter is a relatively easy thing to do if you're not accustomed to writing WP code.
  • Core functions allow some customization through filters. There's probably not a whole lot you could do without writing code. See the docs on Customizing Feeds and the pre_get_posts action. For the former (using a feed) you may need to add something (shortcode, widget, etc.) that displays RSS feeds, referenced to your custom feed. For the latter, you'd probably want to hook to the action and modify the page query if certain conditions are met, like if you're on the page where you want to display your recent posts.

The short version of this is that you'd have a page that shows the results of a query, your recent posts.

Filtering/Sorting By Traffic

This is a separate question and the answer is dependent on how you measure your traffic. It's common to use something like Google Analytics, in which case you'd have to attach to those data to figure out which posts are more popular. I've written code that reads from hosted stat compiliers like AW Stats or Modlogan, so this is possible although not straightforward. You may also be using some other tracking method.

Somewhere in what you do, you'll be running a query of posts, to figure out which posts are the ones you want to use. That will probably give you a list of WP_Post objects. Filtering by date is easy (there's a post_date field and a post_modified field). Since what you're trying to order on (traffic) isn't in the same table, you're probably going to want to correlate your traffic data, probably by post ID or post_name, and reorder the result list (array). See Sorting Arrays in the PHP docs.

Keep in mind that if your data aren't coming from a single query (like you're correlating traffic data), filtering is basically sorting and truncating. That is, pick all possible relevant posts (maybe by date), sort them based on traffic, then cut the list off at the number of records you want. It's not the cheapest procedure from a performance standpoint but if you're doing this once a day or once a week you can cache the results and it should be fine.

Updated: Creating A Snapshot

If you're trying to create a snapshot, you run into two issues:

  1. When (or how) do you create the snapshot? There's a cron API in WP and it just got updated recently. For many use cases, it's probably more than enough. It sounds like this may be all you need.
  2. How do you store and re-display the results? There are plenty of places you can store your query results, like the wp_options table or in a custom post type. Getting the results out again may be more complicated.

Why are you trying to create a snapshot? If it's for performance reasons, an existing cache solution (or some other similar things) will probably be much easier to set up and manage. If it's for auditing, you may be better off using either some auditing solution or simply the content approval already in WP.

  • I guess the output for the list would essentially be like what many of the widgets do for this, display a thumbnail, title link and possibly the excerpt. It's essentially replicating what a sidebar widget does, but creating a snapshot of that info instead of constantly updating that information. – jbwharris May 8 at 15:50
  • How I'm measuring popularity is using WordPress post views. I have a plugin that adds that functionality to our pages. So this internal count would be informing this info. This is how most of those Popular Posts widgets work. – jbwharris May 8 at 15:52
  • 1
    @jbwharris updated. As for WP-PostViews (that's what you're referring to?), getting the data out of that should be no more or less complicated than any other tracking solution like GA or AW Stats. I've not used that plugin before but it looks like it keeps things in your DB, which is good. – Stephan Samuel May 8 at 16:11

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.