Build Your Own Business Website header image

Community News Case Study – Part 14 – Show Only Upcoming Events

Difficulty Level -

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

This is the 14th in a series on using WordPress and Thesis as a Content Management System (CMS). In this case study we are creating a community news site. We have created a static page to display our events sorted by the event date. This page displays all events, regardless of event date. Now we are going to change the custom loop so that it will display only those events that are in the future. We do this by using PHP functions strtotime(), time() to compare dates.

Video Transcripts

Now what we’re going to do tonight is we are going to now create a loop for this that will not show those events that have already expired, that have already come and gone. So I have this full events listing page and we’re going to create another page and it’s going to be called upcoming events.

And so let’s see, let’s log in to this thing. Wp admin and add a new page and we’re going to call this upcoming events. And so, these are events… I can’t type tonight…that are coming up. And so, we’re going to go ahead and publish this and then we’ll go to the Site Options under Thesis and do our pages thing here. And instead of calling this one events, now we’ll call it all and I’m going to turn it off. And then we’re going to use this upcoming events one instead and I am going to just call it events. So now, this page is what we have on our… that was the page that we now have. Where do we go? Okay, that now shows up under events. Now it’s this one instead of the one we had before which was… let’s see, what was that? That was… what was the page? Full events listing, that’s right. So I’m going to open up that in another… open link in a new tab, in another tab and instead of Edmonds News, it’s going to be full events listing.

Okay so the full events listing has everything, even the stuff that has expired and the upcoming events is only going to have the new stuff. And quickly, I forgot to get the page id for this. This is page id 361 so that’s what we’re going to use in our function, 361.

Okay so this morning, we have this loop that we created called all events listing and we’re going to just copy it and then change a few things. So copy, paste and this function creates the upcoming events. Events listing page, instead of all events, we’ll just rename that upcoming. And then we’ll take that function name and we’ll paste it down in the add action function. Okay so upcoming events listing.

Now instead of if is page full events listing, we’re going to say if is page and… what did I say? 361, right? Actually, I don’t even think we need to put in parentheses. I think I’m going to say 361. If is page 361… let’s just double check that. We’ll do that over at the codex so that is conditional tags and conditional tags in the codex and we’re going to look at a page… page type. So if is page… no, it definitely wants to be in quotes. It doesn’t have to be in quotes here. Okay, well anyway, so it doesn’t have to be in quotes. I get confused on that sometimes.

So let’s put it in quotation marks. If is page 361… so we’re still going to… our loop is going to look… it’s going to display all of the posts so it’s post per page equals minus 1. It’s going to take everything out of category 12 which is the weekend events category. It’s going to return the post types Seattle Edmonds in the standard post. We have those 3 post types in this case study then we’re going to order it by meta value and the meta key for that is the custom post meta called event date. And then we are ordering it ascending. And if we were to leave it just like this… well, let’s just do it just like that.

Let’s save that document, let’s open up Filezilla and go to… what did I say? Community, right. Themes, thesis_18, custom and then custom functions php… we’ll just upload that. Now that events page… this upcoming events page should display exactly the same thing as the full events listing. There it is so here’s the one in May, it still exists. Here’s the one down in August, it has happened yet. So it’s doing exactly the same thing.

Now what we want to do is not display the ones that have already passed. So we go back to our function and in our function here… this is what we’re going to do. We are going to… what we’re going to do is we are going to check or get the post id and then we’re going to get the event date of that post and then we’re going to compare timestamps. And so the first thing that we are going to do is actually, we’re going to grab the event date. It’s not actually the first thing but I want to just see where this is coming from. Okay so the event date equals get post meta, post id, event date and then true. We want this to get the post id that’s inside this loop. Now this is actually not going to do it as it does down here. It does down here just fine but it doesn’t work inside the loop like this. And then we’re going to grab the post meta event date and we just want it returned as a string.

So instead of post id like this, we’re going to give it a variable post id. And then we need to get that variable and this is a WordPress function called get the id. And we want to put that in post date, in post id so we’ll grab the variable, copy it, put it right here and then say equals. So post id is now going to be equal to get the id which is what you use inside of a loop like this. It’s going to write to that post id and then we’re going to grab the event date from the post meta using this post id. Okay, so far so good.

Now the next thing we want to do is we want to compare the date with the current date stamp. And so this is going to actually be an if question so if… pardon me. If and then we’re going to use the function called time… if time and this is a php function that gets the current time right now. So if time is less than or equal to the event date then we want to print this. So if it’s less than or equal to event date then we are going to put this Thesis teaser in place. And if it isn’t, nothing’s going to happen. Now unfortunately, the variable here is in a string format and it can’t be accurately compared to time. So what we have to do is we have to take this string format of a date and we have to turn it into a time date stamp and that is a function called string to time or str to time.

Okay so string to time and that’s going to take the time or the string to parse. That is what is happening right here and we don’t need the 2nd parameter which is now because we know it’s now. Okay so if this time is less than or equal to string to time event date then display the teaser. Otherwise, it’s not going to display the teaser. Let’s go ahead and save that and upload it.

Okay and so… let’s see. Let’s check it now. So here we are and we can see our May version here. Now if we refresh this, we no longer have the May version. Now we only have the June 4h 2011.

0 Comments… add one

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
THIS IS A LIMITED TIME OFFER!
Subscription Options
0 comments… add one

Leave a Comment