AnonymousInactive
Hello,
At the moment i am building a website where people can filter posts. Once filtered it is the intention that it should be possible for people to leave their emailadress. Once they do that i save the “$sf_current_query->get_search_term()” and “$sf_current_query->get_array()” to the database to make it possible to repeat the query.
Would it be possible to use the saved data to query the results later on? if so with which function? I already tried “set_field_values_array()” but i cannot get it to work.
The results do not need to be filtered, i just want to check if there are any results afterwords with WP_Query.
Hope that you can help me with the above, thanks in advance!
AnonymousInactive
I tried following this: https://searchandfilter.com/documentation/accessing-search-data/
on this page: http://staging2.iconcasterwheels.com/shop/?_sfm_caster-wheel-size=10%22&_sfm_caster-wheel-material=Air%2FFlat%20Free&_sfm_caster-fastening-type=Top%20Plate&_sfm_caster-load-capacity=200+300
Here is the code:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(56)->current_query();
//var_dump($sf_current_query->get_array());
$searchArgs = array(
"str" => '%2$s',
"delim" => array(", ", " - "),
"field_delim" => ', ',
"show_all_if_empty" => false
);
echo $sf_current_query->get_fields_html(
array(
"_sfm_caster-wheel-size",
"_sfm_caster-wheel-material",
"_sfm_caster-fastening-type",
"_sfm_caster-load-capacity"
),
$searchArgs
);
When I do a var_dump, it shows the info that is expected (hidden for now) — so the id and everything seems to be correct.
However, when I try to display any of the field values, nothing shows up.
No errors in console message or anything either.
I am trying to get it to say “Search Results 10″, Air Flat/Free, Top Plate”
If you are using Ajax, you need these filter labels inside the Ajax Container. If you want them to be shown elsewhere on the page, you still have them, but hidden with CSS, inside that Ajax Container, but use jQuery to copy and paste them to where they will show after our sf:ajaxfinish event has triggered.
You will also need to be using a PHP template for the page. It will be much harder (or not possible at all) if you are using a page builder or post grid plugin/widget.
It will require some coding for which I can point you in the direction of snippets, but I cannot help you write the code, sorry, as this is not within the scope of our support.
To fetch the search terms, the https://searchandfilter.com/documentation/accessing-search-data/ guide is basic but you can extend the idea to grab lots of other data. If you have other filters, then it becomes a little more complex, but I can give you links. This thread might help you:
https://support.searchandfilter.com/forums/topic/accessing-field-slug-on-search-results/
… and this search will give similar threads I think:
https://support.searchandfilter.com/forums/search/sf_current_query+get_array
This particular post may help:
https://support.searchandfilter.com/forums/topic/acf-post-meta-fields-in-current_query-result/#post-249704
I would add that we plan to make this far easier in V3 of our plugin, due in a few months.
AnonymousInactive
Hi 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();
}
AnonymousInactive
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?
You would need to code some PHP. As you are using the Shortcode display results method, start with this guide (if you have not already):
https://searchandfilter.com/documentation/search-results/using-a-shortcode/#customising-the-results
To fetch the search terms, the https://searchandfilter.com/documentation/accessing-search-data/ guide is basic but you can extend the idea to grab lots of other data. If you have other filters, then it becomes a little more complex, but I can give you links. This thread might help you:
https://support.searchandfilter.com/forums/topic/accessing-field-slug-on-search-results/
… and this search will give similar threads I think:
https://support.searchandfilter.com/forums/search/sf_current_query+get_array
Note, if you are using Ajax refreshing of the results, any PHP needs to be inside the Ajax Container, or it will not update.
The code you would need is:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(9490)->current_query()->get_array();
echo $sf_current_query['_sfm_UnitLocation']['active_terms'][0]['name'];
Look carefully, as only the ‘global’ line is the same.
Note, if you are using Ajax on the page, you may need to make sure that the page title is inside the Ajax Container, otherwise it will not refresh and change.
More complex is needed then.
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query()->get_array();
$search_cat = $sf_current_query['_sft_category']['active_terms'][0]['name'];
?>
<header class="searсh-header">
<h1 class="page-title">Résultats de la recherche : <span><?php echo $search_cat;?></span></h1></header>
You first need to find out where the data is, and to do so you need to examine the filter array:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1526)->current_query()->get_array();
echo '<pre>',print_r($sf_current_query,true),'</pre>';
?>
This will print that array to the screen. From that you would need to use code LIKE this:
echo $sf_current_query['_sft_category']['active_terms'][0]['name']
This would echo out the first Category term. You will need to write a similar line, for example:
echo $sf_current_query['_sfm_industries']['active_terms'][0]['name']
Can you try adding this code to the page, so you can see what the query is actually sending, if anything (it might actually give you the variable to use as well):
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(102003)->current_query()->get_array();
echo '<pre>',print_r($sf_current_query,true),'</pre>';
?>
Please note that we are now closed for the day as it is late afternoon here in the UK, so my next reply may not be until tomorrow.