Customizing the Default Query

By default query, we mean the query WordPress runs based on the URL or slug of the page. If we are visiting the blogs archive page, /blogs, WordPress knows it has to query for the blog posts. On the archive page for custom post type events, /events, WordPress automatically queries all the events.

To show event posts on the homepage, we used a custom query to order the posts according to the event date while filtering out events from the past. For the event archive page, we want a similar query which lists all upcoming events. Right now the events archive page lists all the events according to the date the event post was created.

While we can copy the custom query code from front-page.php to archive-events.php, we do not need a custom query when the default query for /events can be tweaked. On the homepage, we needed the custom query because WordPress was not going to query for events posts by default. Here, the default query is already fetching the most recent event posts.

Displaying posts on the event archive page

Open archive-events.php. The code from the while loop is reproduced below. For explanation, see Create Custom Fields for Event Posts lesson.

Get hands-on with 1200+ tech skills courses.