Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created October 27, 2017 13:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djrmom/5bd43ee83acc82048bb0dc5e0e52fde5 to your computer and use it in GitHub Desktop.
Save djrmom/5bd43ee83acc82048bb0dc5e0e52fde5 to your computer and use it in GitHub Desktop.
facetwp date source converted to year
<?php
/**
* reindex after adding or updating this filter
*/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'date_as_year' == $params['facet_name'] ) { // change date_as_year to name of your facet
$raw_value = $params['facet_value'];
$params['facet_value'] = date( 'Y', strtotime( $raw_value ) );
$params['facet_display_value'] = $params['facet_value'];
}
return $params;
}, 10, 2 );
@BryanBarrera
Copy link

@djrmom Thanks for this!

@MWDelaney
Copy link

For real. I need to do this and yours is the first Google result. Thank you!

@nicklace
Copy link

Thanks a lot for this one ;)

@vik-nov-dev
Copy link

Thanks, saved some time and effort!

@nikhilt88
Copy link

Thanks for this idea. I modified it to take the month out of the date but the sorting then happens alphabetically. Would you know how I can sort it the way the months really are? Thanks.

@jahazeb112
Copy link

How to Show 3 months calendar(current month and coming 2 months) instead of the date-picker input field. I am stuck :(

@JakeGonzales
Copy link

JakeGonzales commented Feb 20, 2023

Amazing I just needed this to convert an Advanced Custom Fields (ACF) field date into one that FacetWP could understand.

/**
 * Change ACF field dates to be format that FacetWP understands.
 * Re-index dates in this format.
 * 
 * "Make sure custom field values are stored as YYYY-MM-DD or YYYY-MM-DD HH:MM:SS"
 * 
 * @source https://gist.github.com/djrmom/5bd43ee83acc82048bb0dc5e0e52fde5
 * @source https://facetwp.com/help-center/facets/facet-types/date-range/
 * @since x.x.x
 */
add_filter( 'facetwp_index_row', function( $params, $class ) {

	$facet_name = 'date_range';

	if ( $facet_name == $params['facet_name'] ) { 
		$raw_value = $params['facet_value'];
		$params['facet_value'] = date( 'Y-m-d', strtotime( $raw_value ) );
		$params['facet_display_value'] = $params['facet_value'];
	}
	return $params;
}, 10, 2 );

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