1

I have a blog category called 'Tarot' and within this category I have several (but not all) blog posts that begin with the title 'Tarot Card Of The Month:', followed by the subject of that particular post.

Is there a way, using Wordpress functions, to be able to display a link to a single blog post from this pool of configuration, that changes on a monthly basis?

I know I can easily display a single post from this category using get_posts(), but I don't see any way to be able to circulate this on a monthly basis, and also get the collection of posts based on a snippet of the title.

Is this possible?

  • Are you each month selecting a random "Tarot Card of The Month" post from the Tarot category? Or do you take the latest post from that category each month? What if you don't add a new post to that category each month? Why not tag these posts with "Tarot Card Of The Month" or as a category, instead of searching for this kind of post title in the "Tarot" category? – birgire Sep 22 '16 at 10:34
  • Tags might be a good way of doing it actually. A random selection is ideally what I'm after. Okay so Tags are fine, do you know of have any idea how I could make it automatically change each month? – Lee Sep 22 '16 at 10:36
1

Here are few suggestions if you use a tag for the "Tarot Card Of The Month" posts and want to randomly select a single post each month:

  • It's possible to order by random seed (see ticket here) in WP_Query. You could e.g. try it with the current month number.

  • Note that ordering by random does not scale well. A semi-workaround, for huge number of posts, could be to fetch first n posts and order them by random with PHP. Maybe change the order ASC/DESC by random as well? But there are lot's of tricks out there.

  • Another possibility is to store the result from your random Tarot query with transients with ttl as 1 month. This might not be very accurate as the number of seconds in each month varies.

  • We could also check the current month number, before running the Tarot query. E.g. store it in the options table and when it's different than the stored value, we update the stored query result.

  • Yet another approach could be to update the query results using wp-cron or unix cron for better accuracy.

There are few examples here that might be helpful.

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.