Build Your Own Business Website header image

Community Library Website Case Study – Part 46, Display Posts from the Last 60 Days

Difficulty Level -

Filed Under Topics - ,

Listed Under Lesson Subjects -

Applies to - ,

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 44th in a series of live lessons on using WordPress and Thesis as a Content Management System (CMS). In this lesson we modify the custom loop for our custom catalog page. This custom loop will display recent posts organized by custom taxonomy. We create a “date filter” that will allow us to display all posts from the last 60 days.

Video Transcript

That brings us back to add the date filter to this loop. Now unfortunately, WordPress does not actually have a date filter. So if we type wp query… or WordPress doesn’t have what you’d like it to have for date.

If we go to the wp query and we look at its potential arguments and we look at let’s see, not order by but time parameters you know, you can say show dates of this year or show dates of this month or show dates of this week or this day or this hour and so on and so forth. But what you can’t do… and you can say from March 1st to March 15th but what you can’t do is say for the last 30 days or the last 60 days or the last 90 days. And of course, we want the last 60 days.

And so, what we’re going to do is we are going to… rather than using a query variable which is what we’ve been doing, we are going to use a filter. And that filter, what that filter is going to do is it’s going to change the MySQL statement and it’s going to add this value which essentially says go back 30 days. And you could actually change everything you know, everything in the query this way if you wanted to but it’s the hard way to do it. But nevertheless, it’s the only way to do it in WordPress for you know, something a date that’s not defined, a date that’s essentially calculated from today’s date or any other given period of time.

So we’re going to say the last… we’re going to take this filter and we’re going to put it in our function. And actually, we’re not putting it in our function, it is its own separate function. And right now, if we uploaded this, this would apply to every single instance of the query which we don’t want. We don’t want it to apply to every instance of the query which means that we need to include this add filter in a conditional statement that says you know, only do it in this case. Well you know, it seems to me like the best way to do that is just to come up here in our query and do it.

And so, we have my query equals new wp query. So I’m suggesting that we just put that filter here. And now this filter is only going to be added when this query is being called. Otherwise, if this function isn’t being called, this filter isn’t being called. So this filter’s going to be called every time the query is being called but that’s only going to get called when this page is being called or this function’s being called.

So we’re going to add the filter where… we’re going to add this filter post where with the name filter where and that’s what’s happening here. So we’re doing it to 30 days. Now unfortunately… well, I’m just going to show you. We’ll go ahead and save this and we’ll upload it… it’s not what I wanted. We’ll upload it and we’ll take a look at it. What’s going to happen is you won’t see any posts. And the reason you won’t see any posts is because I don’t have any posts. I don’t have any catalog items that are less than 30 days old, 3 days or less old. And so now, it’s not showing anything.

So what I have to do is I have to change that for my data set, I can’t use 3 days or 60 days. I actually have to use 150 day because that’s how old my data set is. So in this area, I put 150 and I save the document and I upload the document and we check it.

Now every… now what happens is I don’t have any books that meet the 150 day reference. So we’d just write audiobooks. I’ve got Bible and Reference that do, I’ve got 3 books there so it puts it under Bible and Reference. I’ve got 3 books under… or 2 books under Biographies, none under children’s ebooks but I’ve got one under DVDs. A couple of them under fiction or more than a couple under fiction… several under fiction, none under holidays, none under junior fiction, none under junior nonfiction. Now, we’re back to Missions and then nonfiction.

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