File: /var/www/hcv/wp-content/themes/hockey/pages/free-drills.php
<?php
/*
* Template Name: Free Drills
*
*/
$context = Timber::context();
$context['reset_url'] = strtok($_SERVER['REQUEST_URI'], '?');
if(empty($_GET["age"])){
$filter_age = null;
}else{
$filter_age = $_GET["age"];
}
$context['filter_age'] = $filter_age;
$ages = get_terms([
'taxonomy' => 'free_drill_age',
'hide_empty' => true,
]);
foreach ($ages as $age){
if($filter_age){
$age->checked = in_array((string)$age->slug, explode(',', $filter_age));
}else{
$age->checked = false;
}
}
$context['ages'] = $ages;
if(empty($_GET["cat"])){
$filter_cat = null;
}else{
$filter_cat = $_GET["cat"];
}
$context['filter_cat'] = $filter_cat;
$categories = get_terms([
'taxonomy' => 'free_drill_categories',
'hide_empty' => true
]);
foreach ($categories as $cat){
if($filter_cat){
$cat->checked = in_array((string)$cat->slug, explode(',', $filter_cat));
}else{
$cat->checked = false;
}
}
$context['categories'] = $categories;
$tax_query = [];
if($filter_age){
array_push($tax_query, array(
'taxonomy' => 'free_drill_age',
'field' => 'slug',
'terms' => explode(',', $filter_age),
));
}
if($filter_cat){
array_push($tax_query, array(
'taxonomy' => 'free_drill_categories',
'field' => 'slug',
'terms' => explode(',', $filter_cat),
'operator' => 'AND',
));
}
global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$args = array(
'numberposts' => 9,
'post_type' => 'free_drill',
'paged' => $paged
);
if(count($tax_query) > 0){
$tax_query['relation'] = 'AND';
$args['tax_query'] = $tax_query;
}
$drills = new Timber\PostQuery($args);
foreach ($drills as $drill){
//Compare dates
$post_date = strtotime($drill->post_date);
$two_weeks_ago = strtotime("-14 days");
$drill->is_new = $post_date > $two_weeks_ago;
}
$context['posts'] = $drills;
$context['pagination'] = $context['posts']->pagination(10000);
Timber::render( array( 'pages/free-drills.twig', 'page.twig' ), $context );