• Hi,

    I am building a site that requires one random post to be displayed on the home page, but the post should remain there until the end of the day. Then when the next day ticks over, another random post is chosen and it remains there for 24 hours and so on.

    Has anyone come across a plug (or hack) that can accomplish this? Or think of a way to do it?

    Many thanks in advance!
    Bryan

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter huntr77

    (@huntr77)

    Can anyone help me with this please?

    I’m trying to accomplish the same thing and am finding limited info on how to create the timing aspect of this. I have found a few ways to create the random post part but no idea how to call just one random post per day. I’m sure it can be done. Can anyone help us?

    Thread Starter huntr77

    (@huntr77)

    hey akaalvin,

    I ended up having to write the code myself. it was quite a mission and requires javascript to be enabled.

    I had to create a new table in the WP database. I called mine wp_todaysid. The fields are ID, storedDate and randomID

    When a user visits the site, it loads the main site index.php and gets today’s date from the browser (using Javascrpt – client side headers don’t contain date info). Then it checks the above table to see if that date is already there. If it is (i.e. someone has visited the site that day), then use the date and the corresponding ID to display that post. If the date is a new one (for that day), then lookup all WP posts IDs (filtering out any unwanted pages, posts etc). Generate a random ID from all those IDs. Put the random ID into the wp_todaysid table. Then place that ID into a session variable. Then load the index.php file for your template and display that post.

    This works well because it doesn’t matter what time zone a person is in. The key thing is the date that Javascript returns is the local system date. If they’re in New Zealand and 12 hours ahead of the UK for example, the UK users will still be looking for yesterday’s date. By the time the UK users gets to the next day, the NZ users will have generated a random ID. It means that Kiwis might always be generating IDs, but early bird gets the worm! And it’s all random anyway…

    Here is the process the site index.php file goes through to handle the date:

    – Get local date from browser (using Javascript)
    – Because you can’t pass the Javascript date variable to the PHP (the script has already executed), you have to reload the page, passing the date to the URL
    – If date exists in the URL, get it and store in session variable. Reload page.
    – Third reload checks for session variable and continues.
    – Using date from session variable, connect to database and check wp-todaysID table for today’s date
    – If date exists in table, load template index.php file using the post ID corresponding to today’s date
    – If date doesn’t exist, generate a random id, store ID and today’s date in wp-todaysID table. Then load your template index.php file using the post ID corresponding to today’s date

    If you want to send me your email, I can send the code across

    Regards,
    Bryan

    Bryan, thanks so very much for taking the time for this reply. Your answer to things is genius, thanks for doing all the heavy lifting. I would really appreciate the code for sure so i can give it a whirl. Many, many thanks.

    [email protected]

    al

    Thread Starter huntr77

    (@huntr77)

    Just sent it through, although the email address I received in my inbox differs from the one above. Perhaps you updated the post…

    Let me know if you didn’t get the email.

    Hi huntr77
    can you send me also the code?
    please its [Email removed]

    thanks , I really need , I cant find any solutions the whole day.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to display a random post but keep it for the whole day’ is closed to new replies.