Forums › Forums › Search & Filter Pro › Get active applied filters and search keyword
Tagged: filters active search
- This topic has 6 replies, 2 voices, and was last updated 4 years, 2 months ago by Trevor.
-
Anonymous(Private) September 17, 2020 at 2:52 pm #259932
I found this class that allows to output the current filters
<?php //Get an array of objects containing data for the current search/filter //replace <code>1526</code> with the ID of your search form global $searchandfilter; $sf_current_query = $searchandfilter->get(1526)->current_query(); var_dump($sf_current_query->get_array()); ?>
But what I’m really looking for is to dynamically display the filter terms AND search term on Submit (AJAX enabled). Also the ability to Clear those Filters / Search items one by one (X them out).
Please let me know if this is doable?
Trevor(Private) September 17, 2020 at 3:33 pm #259937If your results are being output by a page builder or grid plugin, this is not possible. Similarly, themes that are using frameworks, instead of traditional templates, you would need to use our Shortcode results method, which uses a PHP template.
This post explains a bit for you. Note, the code would have to go INSIDE the Ajax Container in the PHP template:
Anonymous(Private) September 17, 2020 at 3:58 pm #259944Hi Trevor,
I am not using a page builder or anything. I am using a CPT archive page and everything works fine. Even the results of “$searchandfilter->get(ID)->current_query()” are shown ON PAGE RELOAD which I’m trying to avoid since I want to load the results using AJAX.
Here is a screenshot of what I’m trying to achieve along with the use case:
https://share.getcloudapp.com/rRu7ZZzl– When a user selects a Year -> this would add an active filter pill with the selected year (example: 2020) – Same goes for the rest of the filters and the search input field.
– When a user hits the X on the active filter pill, it needs to remove that from the filters and resubmits the form.Thank you in advance!
Trevor(Private) September 17, 2020 at 4:32 pm #259950The ‘X to remove it’ would not be possible to do (I have not had reports of any user being able to achieve this). We will introduce this general feature (the display of filters etc) sometime in V3. Yes, the code I gave you enables you to see the filters on the page so that you can work out where from the array they come so you can grab what terms you need. You have to write the PHP/HTML to output them, as that is not within the scope of our support, or get a coder to help you with that.
V3 will ship in a few months time, but I do not know exactly when, nor which features will be in V3.0 and which ones will appear later.
Anonymous(Private) September 17, 2020 at 5:26 pm #259956Hi Trevor,
Thank you for the response. The code you sent works well, however, it needs to be in the archive page itself to work, which is something I’m trying to avoid because that would mean the page has to reload to display the active filters.I tried to run it through AJAX once the form submits. But I’m not getting any results. (below is the function I added to functions.php) (the ajax request works fine, so the issue is with $searchandfilter).
Is there any way to use the “$searchandfilter” class and the “current_query()” method from OUTSIDE the archive page?
Please let me know. thank you.
add_action( 'wp_ajax_show_active_filters', 'show_active_filters' ); add_action( 'wp_ajax_nopriv_show_active_filters', 'show_active_filters' ); function show_active_filters(){ global $searchandfilter; $sf_current_query = $searchandfilter->get(8175)->current_query()->get_array(); ob_start(); echo '<span class="active-pill">'.$sf_current_query['_sft_contest-years']['active_terms'][0]['name'].'</span>'; $ajax_filters_html .= ob_get_contents(); ob_end_clean(); echo json_encode(array('html'=>$ajax_filters_html)); wp_die(); }
Trevor(Private) September 21, 2020 at 1:51 pm #260155It can only work in the archive template, sorry. It should be run inside the ajax container for it to refresh on ajax reload. You could have the values hidden inside the Ajax Container, and then use our sf:ajaxfinish JavaScript event to use JavaScript to copy the values to where you are using them.
-
AuthorPosts