Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Jonathan Watson
PLUS
Jonathan Watson
Courses Plus Student 2,451 Points

Trying to add an if comparison for a WP_Query loop

Hey guys!

 

I'm trying to load a custom loop based on an if statement and can't quite figure out where it needs to go or how to go about it.

 

What I'm trying to do is compare two variables, so that only posts where $currentdatetime is less than $eventtimestamp are actually returned on the page.

 

I can get my IF to run INSIDE the loop (see line 88) but not as an argument of the loop itself.

 

Gist available here, if any of you wonderful, wonderful techno-gods can help:

 

https://gist.github.com/jonwatson87/2fef22a03b4215384828

Jonathan Watson
Jonathan Watson
Courses Plus Student 2,451 Points

I was sure I tried that, but obviously had something awry or in the wrong place!

 

I've just tried it again (moved the if statement up to line 42 and the endif down to 142) and it's working! XD

 

Thanks for the help Colin Marshall! Much obliged to your keen eyes, m'lord! Never hurts having some else double check for glaring/idiotic mistakes! XD

Jonathan Watson
Jonathan Watson
Courses Plus Student 2,451 Points

P.S. For anyone wondering what changes I made, I've uploaded the updated file to the gist above with the filename archive-events-v2.php.

If my own learning process can help someone else then great! :)

Jonathan Watson
Jonathan Watson
Courses Plus Student 2,451 Points

P.P.S. Colin Marshall - leave your comment as an answer and I'll vote you up! ;)

Colin Marshall
Colin Marshall
32,861 Points

Nice! Glad we got it working. Comment moved to answer.

1 Answer

Colin Marshall
Colin Marshall
32,861 Points

Couldn't you just move that if statement up towards the top of the inside of the query loop and put all the code that puts anything on the page inside of that if statement that checks the dates? That way nothing is printed to the page if the post does not meet those conditions.

I'm sure there is a different (and probably better) way of doing this, but it should work.