Skip Navigation

[Resolved] Execute a Filter View with JS

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to setup taxonomy filter field default value in custom search form?

Solution:

It does not needs custom JS codes, for example:

https://toolset.com/forums/topic/execute-a-filter-view-with-js/#post-2301719

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 2 years, 9 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by MarcoS3712 2 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#2299087

Hello,
I've set a Custom JS code in a View in order to pre-select a filter taxonomy value.
You can see the view here: hidden link
Please, click in FILTER SEARCH to view the filter.

For "Post Tipology" I would like to see posts with "SOLUTION" when page is loading.
And seem to works, but how may I execute the filter when page is loading (in order to see only Objectives by Role tab post)?

This is the code I've set:

jQuery(document).ready(function() {
jQuery("select[name^='wpv-tipologia-articolo'] option[value='solution']").attr("selected","selected");
});

Thanks in advance, Marco

#2299675

Hello,

I assume you are using AJAX search feature, if it is, you can trigger your custom JS codes with event "js_event_wpv_parametric_search_results_updated", for example:

jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
	/**
	* data.view_unique_id (string) The View unique ID hash
	* data.layout (object) The jQuery object for the View layout wrapper
	*/
	// here setup your custom JS codes
});
#2299771
Filter View JS Code.png

Many thanks Luo Yang for your reply.
In the Custom Search View I use Full page refresh when clicking Submit, and I've added JS code in order to pre-select a taxonomy value.
Then, I would like to auto-submit the filter via JS code.

Sorry, but I'm not so confident with JS; I tried with this code solutions, but doesn't works:

SOLUTION 1 (View ID = 4997)
jQuery(document).ready(function() {
jQuery("select[name^='wpv-tipologia-articolo'] option[value='solution']").attr("selected","selected");
});
jQuery(document).ready(function($){
$('form.wpv-filter-4997 select').change(function() {
$('form.wpv-filter-4997').submit();
});
});

SOLUTION 2
jQuery(document).ready(function() {
jQuery("select[name^='wpv-tipologia-articolo'] option[value='solution']").attr("selected","selected");
jQuery('wpv-tipologia-articolo').on('change', function(event) {
jQuery('[name="wpv_filter_submit"]').trigger('click');
});
});

SOLUTION 3
Where Wpv-filter-4997 data-viewhash = 2b14b35850d895e6a2522bd4f0cfe3d6

jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
data.view_unique_id ("2b14b35850d895e6a2522bd4f0cfe3d6")
jQuery(document).ready(function() {
jQuery("select[name^='wpv-tipologia-articolo'] option[value='solution']").attr("selected","selected");
});
});

SOLUTION 4
Where wpv-view-output data-toolset-views-view-editor = eyJuYW1lIjoiY29ub3NjZW56ZWVjb25vbWlhIn0=

jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
data.view_unique_id ("eyJuYW1lIjoiY29ub3NjZW56ZWVjb25vbWlhIn0=")
jQuery(document).ready(function() {
jQuery("select[name^='wpv-tipologia-articolo'] option[value='solution']").attr("selected","selected");
});
});

#2300565

Since it is a custom JS codes problem, please provide a test site with the same problem, fill below private message box with login details, also point out the problem page URL and view URL, I need test and debug it in a live website.

#2301719

I assume you are going to display only "Solution" post by default.

It does not needs custom JS codes, you can try "wpv_filter_query", like this:
Dashboard-> Toolset-> Settings-> Custom code:
hidden link

Add one item, with below codes:

add_filter('wpv_filter_query', function($query, $settings, $view_id){
    if( in_array( $view_id, array(4997) ) && !isset( $_GET['wpv-tipologia-articolo']) ){
      $_GET['wpv-tipologia-articolo'] = 'solution';
    }
  	return $query;
}, 9, 3);

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2301819

My issue is resolved now. Thank you!