Reputation: 53
I'm designing a website for a local performing arts venue, and I would like to add some code so that specific event posts on the "Events" page will automatically be moved from "Events" to a different "Past Performances" page after the date of the event has passed. I've looked around for any existing solution to this query and haven't yet found one.
Upvotes: 3
Views: 147
Reputation: 167
Create a child theme or add page-events.php and page-past-performances.php, you may copy/paste your current theme page.php code.
Here you may choose 2 options:
Create a special loop for each template. For page-events.php:
<?php
$today = getdate();
$args = array('date_query' => array(
array(
'after' => array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
),
'inclusive' => true
)
));
$query = WP_Query($args);
//Here goes the loop
For page-past-performances.php:
<?php
$today = getdate();
$args = array('date_query' => array(
array(
'before' => array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
),
inclusive => false
)
));
$query = WP_Query($args);
//Here goes the loop
The second option uses the action hook pre_get_posts, it could look something like this (inside your functions.php file):
<?php
add_action('pre_get_posts', 'date_filter');
function date_filter($query) {
if($query->pagename == 'events') {
$query->set('date_query', [[
'after' => //same as above
'inclusive' => true
]]);
}
if($query->pagename == 'past-performances') {
$query->set('date_query', [[
'before' => //same as above
'inclusive' => false
]]);
}
}
?>
Upvotes: 1