Forums › Forums › Search & Filter Pro › year dropdown
- This topic has 26 replies, 8 voices, and was last updated 6 months, 3 weeks ago by Trevor.
-
Anonymous(Private) May 30, 2023 at 3:46 pm #276798
I had this issue. I couldn’t find a solution here, so I created a very hacky javascript workaround that seems to work fine. Sharing it here in case anyone has a similar need to have a yearly dropdown and the developers don’t build it in.
Here goes:
1) Just after the filters in the theme template (index.php in my case) I have this:
<li class=”moveme”>
<select id=”years” class=”sf-input-select”>
<option value=””>All Years</option>
<?php wp_get_archives( array(
‘format’ => ‘option’,
‘type’ => ‘yearly’
)); ?>
</select>This creates a dropdown with all the years for which posts exist, with All Years added at the top.
2) On the back end of the S&F plugin I added a Post Date field, set to date range, and a submit button.
3) In my css stylesheet I hid the Post Date field and the submit button:
.sf-field-submit,
.sf-field-post_date,
#primary > .moveme {
display: none !important;
}I’m also hiding the .moveme element when it first loads.
4) Using jQuery/javascript I move the dropdown into the S&F form (where it becomes visible) on page load. When an option is selected, it populates the hidden Post Date fields with 1st Jan-31st Dec for the chosen year and clicks ‘submit’. The years dropdown gets reset when the form is reset.
$(document).ready( function() {
$(‘.moveme’).insertBefore(‘li.sf-field-post_date’).removeClass(‘moveme’);$(‘#years’).change(function() {
var selectedValue = $(this).val();
if (selectedValue == ” ) {
$(‘.sf-datepicker’).val(”);
} else {
$(‘.sf-datepicker’).eq(0).val( ’01/01/’ + selectedValue.substr(selectedValue.length-5, 4 ));
$(‘.sf-datepicker’).eq(1).val( ’31/12/’ + selectedValue.substr(selectedValue.length-5, 4 ));
$(‘.searchandfilter .sf-field-submit input’).click();
}
});$(‘.search-filter-reset’).click(function() {
$(‘#years’).val(”);
});
});NB. You’ll have to switch the order of 31/12 if you’re using American dates.
Obviously this is sub-optimal, but hey, it works.
Anonymous(Private) November 17, 2023 at 11:13 am #276915Hi, just stumbled across this thread. I need to filter by year (just the year) and would really like to use S&F Pro since I already use it on the page for other filters. Is there any way to disable the datepicker and just use a year dropdown?
People don’t search for posts by date – just by year and it would be nice to combine a simple dropdown with a searchfield + category filter.
thanks
-
AuthorPosts