Skip Navigation

[Resolved] Choosing default value for parametric select filter by date

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 13 replies, has 2 voices.

Last updated by romanB-3 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1193682

Hello,
I have a custom parametric search filter by date, based on this code in functions.php

add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 15 );

add_filter( 'wpv_filter_query', 'filter_postdate_func', 10, 3 );
function filter_postdate_func( $query, $settings, $views_id ) {
     
    if($views_id != 39 || !isset($_GET['date-incident'][0]) || empty($_GET['date-incident'][0])) return $query;
     
    $query['date_query']['after'] = $_GET['date-incident'][0] . ' ago';
    return $query;
}

I have also set up a JS function to choose by default a value of the filter like this :

jQuery(document).ready(function($) {

jQuery("select[name^='date-incident'] option[value='6 months']").attr("selected","selected");

});

The frontend shows the right value in the select, but the query is not applied.
I would like to know how to apply the default value in order to apply this to the query's result.
Thank you.

#1193871

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roman,

Thank you for contacting our support forum.

Could you send me a link to the site so that I can have a look at this for you ?

Thanks,
Shane

#1193887

It is a private site. Please activate private fields.

#1193932

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roman,

Here are the private fields.

#1195596

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roman,

This one seems to not be possible with JS.

This is because the AJAX being used for our filter is expecting a click event. I was trying to see if I could simulate this click event but its actually beyond me.

So is it that you want to have this query pre-applied? Will this always be the pre-applied query ?

Because what we can do is to overwrite the default view query to display your items based on this predefined query and use conditionals so that the date filter still works.

Please let me know.

Thanks,
Shane

#1195860

Hello and thank you,
The "6 months" selection should be pre-applied in deed, but only at initial loading. When the user changes this filter, it's the value he inputs that must apply.

#1196299

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roman,

Thank you for the patience.

I can resolve this for you however we would need to have the query parameters in the URL for this to work.

What I intend to do is to use the views query filter hook to set the default value. This would mean that the page will be loaded with the default value you want.

Then once the filter is applied then the page will update with the new items. Is this ok with you so I can enable the browser history management to have the urls parameters being displayed ?

Alternative to this we can use the jQuery as you have it but then submit the form so the page results will be loaded when the page finish loads.

Please let me know which of the solutions you think you would want to go forward with.

Thanks,
Shane

#1197158

Hello,
I'd rather keep the parameters hidden.
Alternative to this we can use the jQuery as you have it but then submit the form so the page results will be loaded when the page finish loads.
Would this mean the results would be loaded twice ?
Thank you.

#1197354

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roman,

Actually yes, this would mean the page search is going to be loaded twice.

The reason is because in order for us to click the submit button on the form we would need to wait until the page is finish loading and the search has loaded to perform a search with the selected value.

Some compromise would need to be made here in order to get some middle ground.

Thanks,
Shane

#1197358

This is quite odd, and pretty annoying.

I would like you could mark this post as a feature request, consisting in adding a "default:XX" option in the parametric filters.

Thank you.

#1197377

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roman,

What I recommend that you do is to suggest the feature request here
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

An email will be sent directly to the team member that is directly responsible for this.

Thanks,
Shane

#1197382

It's done, thank you.

#1197409

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Awesome.

Not much else I can do from my side here since the request has been submitted.

Thanks,
Shane

#1227842

My issue is resolved now. Thank you!