Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewmcnaughton/b082c056c52ba3d8d59a2bf4885dcce1 to your computer and use it in GitHub Desktop.
Save andrewmcnaughton/b082c056c52ba3d8d59a2bf4885dcce1 to your computer and use it in GitHub Desktop.
Order by Month if this year, otherwise only show year (but also in order)
Current code:
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'date' == $params['facet_name'] ) { // change date_as_year to name of your facet
$raw_value = $params['facet_value'];
$today = strtotime(date( 'Y' ));
$screeningDate = date( 'Y', strtotime( $raw_value ));
$screeningDateYear = strtotime($screeningDate);
if($today == $screeningDateYear) {
$params['facet_value'] = date( 'Y m', strtotime( $raw_value ) );
$params['facet_display_value'] = date( 'F', strtotime( $raw_value ) );
} else {
$params['facet_value'] = date( 'Y', strtotime( $raw_value ) );
$params['facet_display_value'] = date( 'Y', strtotime( $raw_value ) );
}
}
return $params;
}, 10, 2 );
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
if ( 'date' == $facet['name'] ) {
$orderby = 'f.facet_value+0 DESC';
}
return $orderby;
}, 10, 2 );
Current output:
<ul>
<li data-index="0" class="selected highlighted">Any</li>
<li data-index="1" class="">July (5)</li>
<li data-index="2" class="">August (4)</li>
<li data-index="3" class="">September (4)</li>
<li data-index="4" class="">January (4)</li>
<li data-index="5" class="">February (4)</li>
<li data-index="6" class="">March (4)</li>
<li data-index="7" class="">April (5)</li>
<li data-index="8" class="">May (4)</li>
<li data-index="9" class="">June (4)</li>
<li data-index="10" class="">2017 (21)</li>
<li data-index="11" class="">2016 (36)</li>
<li data-index="12" class="">2015 (41)</li>
<li data-index="13" class="">2014 (112)</li>
<li data-index="14" class="">2013 (403)</li>
<li data-index="15" class="">2012 (319)</li>
<li data-index="16" class="">2011 (207)</li>
<li data-index="17" class="last">2010 (143)</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment