WordPress.org

Ready to get started?Download WordPress

Forums

Displaying past & upcoming events separately (2 posts)

  1. Eric
    Member
    Posted 9 months ago #

    Hello world!

    This is my first post in these forums. I'm fairly new to wordpress coding & PHP generally, and I'm at a loss on how to add a particular feature to a custom wordpress site I'm running.

    Currently I have a list of upcoming events (based on a custom event post type) which all display on an archive-event.php page. I'd like to add the ability to see a list of past events.

    I think I understand how to write a query to only show past events based on what I'm doing already. My question is how to create this new page within the template hierarchy of the site?

    I'd like to create some new template for past events (past-events.php or similar) wordpress only looks at certain files based on the template hierarchy.

    My site is live at: http://www.artsrestore.la
    And the code for the events archive page is here: https://gist.github.com/egardner/7324310

    Thanks for reading.

  2. bcworkz
    Member
    Posted 9 months ago #

    The archive-event.php template should still work, you're just changing the query to get old events instead of upcoming. You can change the query a number of ways. To generate an URL with the correct query args, use add_query_arg(). The user clicks on the resulting link and the appropriate events will be listed on the archive-event.php template.

    You could create a custom permalink tag, say "age", where the value is a positive or negative number of months to query for from now by using the Rewrite API.

    You could arbitrarily change the query if certain conditions are met by hooking into the 'pre_get_posts' action.

    You could create a custom page template that does exactly what you want anytime a page based on that template is requested. Even though this causes the main query to be for a single page post type, your template script can run a second event query and display the results.

    There's probably more possibilities. Just be glad we're into 3.7, before now, relative time queries were a pain. The new date_query makes it simple.

Reply

You must log in to post.

About this Topic