Time Since
Filter results by time interval, such as "in the past 30 days"
The Time Since facet type can be used to filter posts by date / time since a specified interval.
When calculating the time intervals, the Time Since add-on honors the timezone you have set on the WordPress General Settings page.
Available options
Name | Description |
---|---|
Data Source | A date field, for example “Post Date”, “Post Modified”. Or a custom field in YYYY-MM-DD format. |
Default label | Change the default “Any” label for the chosen UI type:
Note: this label is translatable with the facetwp_i18n hook. |
Choices | The choices to display (one per line), consisting of a label and a format, separated by a | : label | format . A few examples:
The formatter is based on PHP’s strtotime function. See the available relative date-time formats. The choices labels are translatable with the facetwp_i18n hook. |
UI type | The UI type of this facet:
Note: if you select the fSelect UI type, you can also apply all customizations described on the fSelect facet page, like setting a custom fixed label, changing the search box text, changing the “No results found” text, and hiding the search box. |
Show ghosts | Show choices that would return zero results? If enabled, facet choices (created with the facet’s “Choices” setting) that would return zero results are still shown, but dimmed and not clickable. If disabled, facet choices that have no associated posts will be hidden.
Note: with this setting enabled, you can also (intentionally) create choices that have zero results before filtering, which is slightly different from the behaviour of other facet types’ ghosts, which only show up after filtering. |
Using custom time ranges
Let’s say you wanted to add a choice to filter by Older than 1 year
.
First, add a new row into the “Choices” box:
Older than 1 year | over-1-year
As you can see over-1-year
is the custom formatting token. Since PHP doesn’t know how to automatically parse this, a hook is needed to set the date range manually:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_time_since_date_range', function( $range, $format ) { if ( 'over-1-year' == $format ) { $dt1 = new \DateTime( '1970-01-01' ); $dt2 = new \DateTime( '-1 year' ); $range['lower'] = $dt1->format( 'Y-m-d' ); $range['upper'] = $dt2->format( 'Y-m-d' ); } return $range; }, 10, 2);
Hide counts
How to hide the counts for the Range List facet, depends on the UI type setting:
UI type set to “Radio”
Add the following CSS into your theme’s style.css
. Be aware that this also hides the counts for all facets of type Checkboxes, Radio, Hierarchy, and Range List (with UI type set to “Checkboxes” or “Radio).
How to use custom CSS?
CSS code can be placed in your (child) theme's style.css file. Alternatively, you can add it manually between
<style>
tags in the<head>
section, in your (child) theme's header.php file. You can also load it with a hook in your (child) theme's functions.php file, or in the Custom Hooks add-on. To load the code only on pages with facets, use thefacetwp_scripts
hook. To load it on all pages, usewp_head
orwp_footer
. Or you can use a code snippets plugin. More info.facetwp-counter { display: none; }
If you need to be specific about which (type of) facet to hide the counts for, use any of the following styles:
How to use custom CSS?
CSS code can be placed in your (child) theme's style.css file. Alternatively, you can add it manually between
<style>
tags in the<head>
section, in your (child) theme's header.php file. You can also load it with a hook in your (child) theme's functions.php file, or in the Custom Hooks add-on. To load the code only on pages with facets, use thefacetwp_scripts
hook. To load it on all pages, usewp_head
orwp_footer
. Or you can use a code snippets plugin. More info/* Hide counts in all Time Since facets in UI mode "Radio" */ [data-type="time_since"] .facetwp-counter { display: none; } /* Hide counts in a specific Time Since facet in UI mode "Radio" */ .facetwp-facet-yourfacetname .facetwp-counter { display: none; }
UI type set to “Dropdown” or “fSelect”
To hide counts from all facets of a type that use a dropdown UI (all Time Since and Range List facets with UI type set to “Dropdown” or “fSelect”, Dropdown facets, fSelect facets, and Hierarchy Select facets), add the following to your theme’s functions.php:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_facet_dropdown_show_counts', '__return_false' );
If you want to hide counts from specific facets with a dropdown UI, then use this instead:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_facet_dropdown_show_counts', function( $return, $params ) { if ( 'your_facet_name' == $params['facet']['name'] ) { $return = false; } return $return; }, 10, 2 );
Create a month/year facet
The Time Since add-on cannot be used to create a facet that shows years (or months and years) as facet choices. See this example that shows how to create a month/year facet using the facetwp_index_row
hook.
Changelog
1.7.2
- Improved added "UI type" settings which many new display options
1.7.1
- Fixed proper timezone support
1.7
- New added "UI Type" setting
1.6.7
- New added "Default label" UI setting
- Fixed facet counter CSS class now matches other facets
- Fixed linked to docs in the setting tooltip
1.6.5
- New ability to use custom format ranges (see the doc page)
- Fixed default to "OR" mode for all Time Since choices
1.6.4
- Fixed User Selections facet was empty
- Fixed use JS versioning to bust caching
1.6.3
- Fixed filtering broken when labels are translated using `facetwp_i18n` hook
1.6.2
- Improved changed textdomain to `facetwp-time-since`
- Fixed choice labels can now be translated via the `facetwp_i18n` hook
- Fixed undefined $token variable (props Rhys)
1.6
- New revamped filtering logic to support "today", "tomorrow", and "yesterday"
1.5.1
- Changed replaced `wp.hooks` to `FWP.hooks` due to conflict with WP 5.0