Dating WordPress

This post is going to attempt to document how I figured out how to mess with dates in WordPress custom fields. I don’t know how widely valuable that is but a number of the concepts are probably broadly applicable. This particular discussion will wander into areas of programistan and there is one child theme page involved.1 It’s live and working (although it may occasionally be down when I do something odd and it’s ugly as sin at the moment) but feel free to look around or add some fake events.

Getting all posts with a custom field named ‘event’ to show on the front page was discussed here. I added a bit to the theme so that if there was more than one event on that day it’d display it with a different format. You can see the whole page on GitHub here.

<?php /* The loop */?>
//It is important to count the right variable here $list_of_posts rather than say the_post or other things I counted accidentally
			<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post();?>
			
				<?php // Display content of posts ?>
				<?php //if the number of posts returned is great than one use the page-posties template rather than the page-posty template
				if ($countposts>1){
                                //For now the only real difference is that posties uses a 6 column bootstrap layout and posty uses a 10 column layout but this could be much more
				 get_template_part( 'content', 'page-posties' , 'page-nav'); 
				}
				else
				{
				 get_template_part( 'content', 'page-posty', 'page-nav' );
				}
				?>
			<?php endwhile; ?>

Views of layouts based on number of posts returned

The other little chunk of code that might matter is what’s returned if there are no events on that day. It just tells you nothing is there and presents you with the events for the next seven days as defined by the FacetWP layout (which I’ll go over in a bit).

<?php else : ?>
		<div class="nada">
            There are no events today. Maybe you'd like to see what's going on in the next week?<br />
//remember when doing shortcodes in templates that you need to do it this way           
<?php echo do_shortcode( ' [facetwp template="week"]' ); ?>
            </div>

Now that we had a decent way to see what was going on that day, I built the “Event Creator” form which will allow the instructor (or anyone else you want) to provision posts with the right data. I used Gravity Forms. You can attach all the bells and whistles you’d like but the piece that mattered most for my purposes is simply writing a date to a custom field. That’s dead simple to do in Gravity Forms.
Image of custom field creation in Gravity Forms

I am messing with other options like including a Google map automatically and I’ve used the content template options to structure the post and title but none of that’s fully settled so it’s probably not worth getting into now. If you’re feeling more curious you can download the form as it is now and import it into your own Gravity Forms install.

Now to get down to the more interesting stuff.

FacetWP – Next Level

As I thought about it, I thought it’d be useful to show people content for the upcoming week. I figured most college students don’t plan much farther ahead than that. I also thought it would be useful to be able to parse through the results by event type (film, dance, photography, whatever) and that led me back to FacetWP. I knew I could create another page template that would do what I needed but I wondered what I could run in the FacetWP template. It turns out I can run quite a bit more than I thought.

 <?php
//gets you the date and breaks it up
  $blogtime = current_time( 'mysql' ); 
        list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $blogtime );
        $now = $today_year . '-' . $today_month . '-' . $today_day;

//creates an array to hold the dates for seven days from now
        $now = array($now);

//loops through to write those dates and push them to the array above
for ($x = 0; $x <=7; $x++ ){
    array_push($now, $today_year . '-' . $today_month . '-' . ($today_day++));
    };

return array(
//these first three make sure you get only the first 30 published posts
'post_status'=>'publish',
'post_type' => 'post',
'posts_per_page' => 30,
//sets the meta_value to the array of dates
'meta_value' => $now,
//softs the events from soonest to latest
'orderby' => 'meta_value',
'meta_type' => 'DATE',
'meta_key' => 'event',
'order' => 'ASC'
);

In honor of full transparency and learning for all, that for loop was originally written by hand like below but I knew that was really ugly even as I wrote it. See how much cleaner it is above? No doubt there are additional ways to tidy things up but it’s fun to get better at things and to make things that work.

 
//it works but don't do this
$now = array($now, $today_year . '-' . $today_month . '-' . ($today_day+1), $today_year . '-' . $today_month . '-' . ($today_day+2), $today_year . '-' . $today_month . '-' . ($today_day+3), $today_year . '-' . $today_month . '-' . ($today_day+4), $today_year . '-' . $today_month . '-' . ($today_day+5), $today_year . '-' . $today_month . '-' . ($today_day+6), $today_year . '-' . $today_month . '-' . ($today_day+7));

I can put all that in the FacetWP template under Query Arguments which I didn’t realize was possible prior to doing this. It opens up a whole new world of avoiding child themes for me.
Showing where the query arguments reside in facetwp

That works well to get me the events for the week but I also figured some people might want to see all the future events. That required that I compare today’s date to the event value. This is a meta_query which was a new phrase for me and it took me a bit to figure out how to work it into this particular arrangement. Essentially, you set up a an array for the meta_query and then work it into the wp_query which makes a lot of sense now but didn’t when I started.

<?php
  $blogtime = current_time( 'mysql' ); 
        list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $blogtime );
        $now = $today_year . '-' . $today_month . '-' . $today_day;

//sets up your custom field search, names the field you want and what you want to look for- in this case whenever event>$now
$meta_query_args=array(
                        'relation'  =>   'AND',
                        array(
                             'key' => 'event',
                             'value' => $now,
                             'compare' => '>',
                             )
                        ); 

return array(
'post_status'=>'publish',
'post_type' => 'post',
'posts_per_page' => 30,
'orderby' => 'meta_value',
'meta_type' => 'DATE',
'meta_key' => 'event',
'order' => 'ASC',
//do that meta_query
'meta_query' => $meta_query_args,
);

1 Although I now believe I could avoid that using FacetWP but I’ll leave in there for Alan’s sake.

One thought on “Dating WordPress

Comments are closed.