Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Filter by year (custom dropdown)
- This topic has 3 replies, 2 voices, and was last updated 2 years, 1 month ago by
Ernest Marcinko.
-
AuthorPosts
-
April 26, 2024 at 4:41 pm #47958
nicktf
ParticipantGood 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-filterOtherwise, 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); } }April 26, 2024 at 4:54 pm #47960Ernest Marcinko
KeymasterHi,
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.
April 29, 2024 at 1:17 pm #47970nicktf
ParticipantThank you, that worked. I added some code to generate the post_meta based on the published date, and set the custom field to that.
April 29, 2024 at 3:34 pm #47979Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- You must be logged in to reply to this topic.