We are well on our way to creating a solid, simple Events Page listing for your WordPress Church website.
If you’ve jumping in mid-stream, be sure to start with Part 1, as I’ve outlined the requirements and parameters behind the Events Page:
- Part 1 – Covered the requirements and parameters, and created the Custom Post Type for Events.
- Part 2 – Edited and created the Loop being used by the Event Custom Post Type.
- Part 3 – Created and prepared the Event Page Template.
Today, we will Query the Event Custom Post Type.
Event Page
Before you open up your text editor, let’s open WordPress first.
If you’re not doing this on a local testing environment, I highly recommend it. There’s no need to be hacking on your live site. You can learn how to set it up a local server environment here.
Alright, open up WordPress and go to your new Event Custom Post Type. For testing purposes, let’s create five Events.
As you create each Event, you need to utilize WordPress’ Custom Field by adding, “Date.”
This is the date of the Event being listed. WordPress will read this Value to determine whether or not it should be displayed.
After you create this the first time, it will always be a selection on the Custom Field pull-down menu.
On each of the test Events, be sure to add this info, along with the date (Value). If you don’t add the date, the Event will never display. It is also important to follow the date format: MM/DD/YYYY.
It seems to work fine if you key, 12/5/2011, but if you drop the zero on the month, 5/25/2012, it won’t display. For a general rule of thumb, I recommend simply requiring users to use all the digits, i.e. 05/03/2013.
For test purposes, put two dates that have past, two dates in the future, and one date for, today. This way you can make sure everything is functioning properly.
Query Posts
Now we’re getting fancy. 😉
Look for a line of code that should go something like: <?php if (have_posts() …
You’ll usually find it just below: <div id=”main” … or <div id=”content” …
This will vary on the theme you’re using, but if you’ve nosed around your theme, before, I’m sure you’ll know exactly what to look for.
Make a few extra lines of space between these two line. White space is our friend, especially when you’re playing around like this. You can tighten things up, later.
Drop this in (between the ‘edit’ notations):
[cc lang=”php”]
UPDATE TWO!
As pointed out by Drew Palko, the date must be formatted like so: YYYY/MM/DD
This is because the data is stored as a string.
So, be sure you enter the date in your custom field as YYYY/MM/DD and edit the post query to read:
[cc lang=”php”]’meta_value’ => date(“Y/m/d”),[/cc]
Thank you!
Tom McFarlin says
If you’re nesting a query inside of the main loop, remember to call wp_reset_query at the end of the nested loop :).
Eric Dye says
The Code god has spoken – #WINNING!
Question: Is this ONLY if you have it place in the main loop? Or should this also be done when you’re using a committed loop as outlined in this tutorial?
Tom McFarlin says
It should be used whenever you’re done a custom query (which may or may not be a nested loop). Check it.
Eric Dye says
You’re the best, thanks!
(Post updated.)
Jay says
Where is that line of code for the “Query Posts” located? Not sure where to look…
Eric Dye says
The Query post code is completely new and being added by you. There shouldn’t be a query post line in it before you drop in the code.
Does that make sense?
Lantz Howard says
I am sure it is in the works, but can you drop any hints when the team will be coming out with Church based WordPress theme? 🙂
Ryan says
Would this step be possible using a custom write-panel/meta-box instead of the custom fields section? If so, would you still use the ‘meta_key’ ‘meta_value’ and ‘meta_compare’ in the array?
Eric Dye says
I’m not sure. Give it a try and let me know. I would love a write-panel/meta-box (pulldown date selector would be even better) for this. If you improve this, let me know, and you can share it with us all! 🙂