Filter by year (custom dropdown)

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Filter by year (custom dropdown)

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #47958
    nicktf
    nicktf
    Participant

    Good day, please could you assist in creating a year dropdown filter? The datepicker isn’t ideal, as we only want to be able to search within a year, and the datepicker requires selection of a month and day as well.

    Please note, the server only allows SFTP and requires a port: 57887

    I am working on getting the VPN disabled so that you can gain wp-admin access if need be.

    I have tried to implement code as per the documentation – though it’s quite difficult to understand, because half of it is commented out, and not even sure if adding a dropdown (select>options) is even possible?
    Here is the documentation I tried to follow. I will post my code attempt below. https://knowledgebase.ajaxsearchpro.com/frontend-filters/frontend-filters-api#date-type-custom-field-filter

    Otherwise, I know that jquery UI allows you to change the datepicker to a year using: https://api.jqueryui.com/datepicker/#option-changeYear
    As well as providing a range using: https://api.jqueryui.com/datepicker/#option-yearRange
    In this case we would need to override the initialisation of the datepicker as it’s created.

    // --------------------------------
    // Date filter example https://knowledgebase.ajaxsearchpro.com/frontend-filters/frontend-filters-api#date-type-custom-field-filter
    // --------------------------------
    add_action('asp_pre_parse_filters', 'asp_add_my_own_filters', 10, 2);
    function asp_add_my_own_filters($search_id, $options) {
        if ( $search_id == 1 ) {
            $filter = wd_asp()->front_filters->create(
                'custom_field',
                'Date Picker',
                'dropdown',
                array(
                    'field' => '_date',
                    'placeholder' => 'all',
                    // The display date format
                    'date_format' => 'yy',
                )
            );
            //add years from 2019 to the current year
            $datesArray = array();
            for ($i = 2019; $i <= date('Y'); $i++) {
                //add all
                if($i == 2019) {
                    $datesArray[] = array(
                        'label' => 'All',
                        'value' => '',
                        'selected' => true
                    );
                }
                $datesArray[] = array(
                    'label' => $i,
                    'value' => $i
                );
            }
            $filter->add(
                $datesArray
            );
            $filter->selectByOptions($options);
            wd_asp()->front_filters->add($filter);
        }
    }
    #47960
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    If by any chance the date is stored in a mysql datetime format, or something similar to that – and not as timestamp – then there might be a way to achieve that without a single line of code.

    The idea is to treat the datetime field as a “string”. Datetime is stored like 2024-12-31 23:59:59 – which is very lucky for us, as the year is the only 4 digit string to match.

    Therefore creating a drop-down/radio/checkbox filter with simply the year values like this will actually match the year: https://i.imgur.com/P5zdZvQ.png
    If the field is stored as datetime, this will always match the year, as it’s the only 4 digit value in the field.

    Doing a range type with this method is not possible. However if you were have a separate custom field to only store the year, then you will be able to also use the range slider filter as well.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #47970
    nicktf
    nicktf
    Participant

    Thank you, that worked. I added some code to generate the post_meta based on the published date, and set the custom field to that.

    #47979
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.