Show Today’s Events

carving tools and wp logo

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 );

For instance, I tried to be really clever and use javascript initially. It occurred to me that if I could just pass a “live” date variable as a tag to a my trusty display-posts shortcode then I could avoid writing a child theme. It seemed like something I could do with javascript and it turns out I could. You can see it on Code Pen. It does just what I wanted only I hadn’t thought through that it does it after the post has gone through the cycle where shortcodes would be transformed from caterpillar to butterfly. I still might have a use for it but I did feel pretty foolish. Especially since I worked out a work around that would enable me to create a tag from the date field entry.2 I solved many problems but missed the major one.

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.

2 It involved the #hashtagger plugin and an awkward date format that had to use underscores instead of dashes or slashes as per this.

4 thoughts on “Show Today’s Events

  1. Interesting.Someone could use this to assemble an archive of evergreen “this day in X history” pieces related to their field that would then pop up on the appropriate day.

    1. I thought it would have some possible broader applications and I think I have worked out how to do Jesse’s request as a result of all this mucking around.

  2. Congrats you’ve cracked another chink in the walls of programmistan. Doing custom wp_query leads to all kinds of capabilities (query on a range of dates for example). And next thing i know you will be running multiple loops per page.

    That’s all the display_posts is doing for you, it just converts the short use parameters into a query. And it cannot do all you can by hand.

    Go deeper, feel the query force

Comments are closed.