Forums Forums Search & Filter Pro year dropdown

Viewing 7 posts - 21 through 27 (of 27 total)
  • Anonymous
    #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.

    Trevor
    #276800

    Thanks for sharing.

    Anonymous
    #276915

    Hi, 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

    Trevor
    #276917

    Hi Stefan

    The solutions described in this thread are the only ones currently available to you, I am sorry.

    Anonymous
    #276919

    Hi Trevor, thanks for the reply. I was just hoping for a simpler approach, but the posted solution will do for now.

    Anonymous
    #288952

    Has the featured to sort by Year downdown been added? It’s something that we use almost daily and keep doing workarounds. Would be so useful to have this generated from ACF date field.

    Trevor
    #288954

    I am sorry, not yet.

Viewing 7 posts - 21 through 27 (of 27 total)