In WordPress it’s easy to delay publishing until a certain date or to show posts published on a certain date but I didn’t know of an easy way to show posts associated with a certain date. What I wanted to do was allow an instructor to write a bunch of posts about art related events in the local area. They’ll be browsable in a variety of ways but we wanted the ones that were relevant to today to show up on the home page automatically so they’d get attention etc.
This was one of those scenarios where I say something like “I am confident it can be done but I’ve never done it or seen it done.” In my head it made sense. I tried a few different ways but I’ll start with the one that worked and was pretty easy. It does require that you make a child theme but I plan to make a plugin that’ll do it if you give me a few days. Anyway, here’s the chunk that does the work. It is, as usual, a result of looking through the WordPress Codex. This is the source for the current time and the light finally went on that I didn’t even need to search a particular custom field as I read this piece on wp_query for custom field parameters. It’s dead simple. I kept making it much harder than it needed to be.1
//gets the time and splits it up into pieces $blogtime = current_time( 'mysql' ); list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $blogtime ); //assigns the pieces in the right order with dashes $now = $today_year . '-' . $today_month . '-' . $today_day; $args = array( // Change these category SLUGS or meta_value parameters to suit your use. 'category_name' => 'event', 'paged' => $paged, //retrieves any post with any custom field value that matches the current date as defined in $now 'meta_value' => $now ); $list_of_posts = new WP_Query( $args );
1 If for some reason you wanted to see it in action, the POC display page is here and the form to provision it is here. Feel free to submit stuff if you’re oddly bored/curious as it’s clearly a site for messing with stuff.