Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Last active February 5, 2023 08:37
Show Gist options
  • Save LinzardMac/ae7c17d4615a80f3a31d to your computer and use it in GitHub Desktop.
Save LinzardMac/ae7c17d4615a80f3a31d to your computer and use it in GitHub Desktop.
FacetWP Customized Loop for "dates span" field type
function facet_availability_logic( $return, $params ) {
// get facet in question
$facet = $params['facet'];
// assign values to $selected_values var
$selected_values = $params['selected_values'];
// if the facet's NAME is 'date'
// (this can be changed to the name of your facet you want to override)
if ( 'dates' == $facet['name'] ) {
// assign variables to first (start date) and second (end date) queries passed
$start_date = $selected_values[0];
$end_date = $selected_value[1];
// if the 'end date' field is empty so that we can later
// create a date span of one day and end date is not passed.
// ** can possibly also use isset() or empty() **
if($end_date == ''){
// make $start_date and $end_date the same
$end_date = $start_date;
}else{
// otherwise make $end_date the value of the second param passed
$end_date = $selected_values[1];
}
// run our function and pass the values of $start_date and $end_date to be used
// in our query for posts
$the_post_ids = get_matching_events( $start_date, $end_date );
// return the results
return $the_post_ids;
}
return $return;
}
add_filter( 'facetwp_facet_filter_posts', 'facet_availability_logic', 10, 2 );
function get_matching_events($start_date, $end_date){
// Format dates
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = date('Y-m-d', strtotime($end_date));
// Set start/end as dateTime
$begin = new DateTime( $start_date);
$end = new DateTime($end_date);
// add one day because date spans leave off a day
$end = $end->modify( '+1 day' );
// interval of 1 day
$interval = new DateInterval('P1D');
// set date interval
$daterange = new DatePeriod($begin, $interval ,$end);
// setup array variable
$datesarray = array();
// for every date in our list
foreach($daterange as $date){
// setup an array of formats that match what is stored in our meta field
$datesarray[] = $date->format('Y-m-d');
}
// build your meta query to search for each date as an 'OR' operator
// 'OR' lets us match any single date instead of all dates
// meta field is a JSON list of all dates for this event
$meta_array = array(
'relation' => 'OR', // defaults to "AND"
);
// for every date in our span of dates array
// create a meta query to search for a match for each date in the list
foreach ($spandates as $day) {
$day = array(
'key' => 'custom_date',
'value' => $day,
'compare' => 'LIKE',
);
// add the day meta query arguements to the meta query array
$meta_array[]=$day;
}
// general get_posts arguements
$args = array(
'posts_per_page' => -1,
'post_type' => 'events', // change to your custom post type
'meta_query' => $meta_array // display all of our combined custom meta query stuff here
);
// get posts that match our custom query
$myposts = get_posts( $args );
// set $post_ids as an array variable
$post_ids = array();
// for every post, set up my $post variable
foreach ($myposts as $post) {
// build an array of post ID's returned from my query
$post_ids[] = $post->ID;
}
// return the array of $post_ids to the facet filter
return $post_ids;
}
@gregorywilliam
Copy link

Hi there, was hoping to see if it possible to have set date dropdown options likes today, tomorrow, this month, easter, Christmas etc and compare against acf start date field AND acf end date fields. This looks like it has some code (especially getting the date range as a variable) that could help me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment