As opposed to a cronjob the update the option every day. I also tried to explain a bit of autoloading and the difference between Options and Transients.
I made a note about Transients though as that is an alternative vs him mentioning a cronjob. Either is going to be saved to the options table anyway, but I feel like Transients would be way less of a pain than a cronjob. Plus the docs suggest Transients are optimized for object caching, but as you mentioned the performance boon is almost certainly negligible.
I did update the answer to put a bit more emphasis on just using WP_Query. It's always difficult to balance answering the question vs addressing the underlying symptoms. I know when I ask questions I usually abstract it to condense the issue of what I'm acutally doing. I'm still not sure about timezones though.
Also, I mentioned this in my answer as well, but I wouldn't save the output for flexibility sake and just process the output based on the post ID or post object during the page request.
One of the core problems is that they don't necessarily want the exact "one year ago" post, because that may not exist, which increases the actual request query.
You're right, I added the ingredients as to not bake the whole cake, that's why I put the cache section close to the section that does the post check - but it could use an edit to make that more clear. Otherwise, I would need to group everything so that if the cache is invalid, it does the query etc.
The nice thing about breaking it out of a normal WP_Query loop is you would be able to put the bulk of the functionality into it's own callback so you can separate business logic and display logic. Whereas setup_postdata() will grab the cached post if it's in cache, or it will cache it since there's only 1 post to display.
@TomJNowell I see that I'm not doing a conditional check in my answer (to check for a cache), but caching the post object is not going to invalidate it after midnight. A standard query at every load when trying to determine X posts back just seems inefficient instead of doing it once and caching the result
Hopefully, in time, and as you do more programming it will make more sense but we're here if you ever have questions too. We try to be a helpful bunch, but this chat is less active than say the official web irc or some of the slack instances.
The WP_Query pulls X posts (posts_per_page value) before the provided date. Where the dateDiff is the piece that actually narrows the results to 1 post within 7 days.
Now that you have the one post, you don't need to loop or query any more posts, you just need to setup that one post so you can use template tags such as the_title().
I did run and test the code in my answer, so it should work "out of the box" so to speak. You would just need to update how you're saving and displaying the results.
I think so, you don't want to do anything if $post_to_display is NULL, and if( NULL ) is false, so it should skip that entire section if $post_to_display is null.
So, you do need a WP_Query somewhere. If you have a WP_Query and you didn't add it into your question today, you would var_dump() the $query variable after the WP_Query call