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”
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?
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.
I have edited and tested it for you. This is the code I used in the end:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1949)->current_query()->get_array();
$category = $sf_current_query['_sft_category']['active_terms'][0]["name"];
$county = $sf_current_query["_sfm_county"]["active_terms"][0]["name"];
if ($category) {
echo $category . ", ";
} else {
echo "All Categories, ";
}
if ($county) {
echo $county;
} else {
echo "All Counties";
}
You may edit that to make it look better though.
AnonymousInactive
I do, thanks. However, it’s now outputting the text ‘ArrayAntigonish’ (or ‘Array’ + whatever county is selected) and it’s not outputting category name. I have tried to puzzle through this to see if I can work out how to fix it myself but my PHP just isn’t good enough.
Here’s a link to a sample results page.
What I’d like it to say is something like “You searched for Breweries, Distilleries, and Vineyards in Antigonish County” and then the results.
Below is the full code of results.php. If you can offer any more insight I’d be grateful.
<?php
/**
* Search & Filter Pro
*
* Sample Results Template
*
* @package Search_Filter
* @author Ross Morsali
* @link https://searchandfilter.com
* @copyright 2018 Search & Filter
*
* Note: these templates are not full page templates, rather
* just an encaspulation of the your results loop which should
* be inserted in to other pages by using a shortcode - think
* of it as a template part
*
* This template is an absolute base example showing you what
* you can do, for more customisation see the WordPress docs
* and using template tags -
*
* http://codex.wordpress.org/Template_Tags
*
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( $query->have_posts() )
{
?>
There are <?php echo $query->found_posts; ?> posts that match your search! <br />
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1949)->current_query()->get_array();
echo implode(", ",$sf_current_query['_sft_category']['active_terms']);
$county = $sf_current_query["_sfm_county"]["active_terms"][0]["value"];
if ($county) {
echo $county;
} else {
echo "All Counties";
}
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
</div>
<hr />
<?php
}
?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
<div class="pagination">
<div class="nav-previous"><?php next_posts_link( 'Next page', $query->max_num_pages ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Previous page' ); ?></div>
<?php
/* example code for using the wp_pagenavi plugin */
if (function_exists('wp_pagenavi'))
{
echo "<br />";
wp_pagenavi( array( 'query' => $query ) );
}
?>
</div>
<?php
}
else
{
echo "No Results Found";
}
?>
Ah. Try this then:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1949)->current_query()->get_array();
echo implode(", ",$sf_current_query['_sft_category']['active_terms']);
$county = $sf_current_query["_sfm_county"]["active_terms"][0]["value"];
if ($county) {
echo $county;
} else {
echo "All Counties";
}
I am coding a bit blind here though. Notice how the second line changed, and the way the category output is changed?