AnonymousInactive
Okay, good news, I’m making progress…
This works for the array:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(99801)->current_query();
if ( empty($sf_current_query ) ) {
echo '<strong>Filters</strong>' . ' • ' . "All" . "<br /><br />";
}
else {
$args = array(
"str" => '%2$s',
"delim" => array(", ", " - "),
"field_delim" => ', ',
"show_all_if_empty" => false
);
echo '<strong>Filters</strong>' . ' • ' . $sf_current_query->get_fields_html(array("_sft_cuisine", "_sft_name"), $args);
}
echo '<br />';
?>
Now I need to pull in the $key[‘active_terms’][0][‘name’] from the original code (it will display before the array).
That will give me what I’m after…
Filters • Florida • Caribbean, Mexican
How do I combine it?
AnonymousInactive
I’ve been crunching through the forum, but not seeing an answer.
– – – – – – – –
I’m using the following code to display a list of current terms for the filtered search:
</php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(99801)->current_query()->get_array();
echo '<strong>Filters</strong>';
if ( empty($sf_current_query ) ) {
echo ' • ' . "All" . "<br />";
} else {
foreach($sf_current_query as $key) {
echo ' • ' . $key['active_terms'][0]['name'] . ' ';
}
}
echo "<br /><br />";
?>
– – – – – – –
My filter includes dropdowns and checkboxes. For example:
Market (dropdown)
Florida
Cuisines (checkboxes)
[ ] American
[X] Caribbean
[ ] Italian
[X] Mexican
– – – – – – –
The code works and the list displays, but it only shows one of the selected checkbox terms…
Filters • Florida • Mexican
Instead of…
Filters • Florida • Caribbean • Mexican
OR.. Filters • Florida • Caribbean, Mexican
I suspect that an array is required, but not sure what/where/how.
Please help this newbie out.
Thanks!
But I don’t access it using get_fields_html. I do it like this:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(934)->current_query()->get_array();
$my_post_type = $sf_current_query[post_type][active_terms][0][value];
AnonymousInactive
I think I’ve sorted it thanks Trevor – sorry to mess you around!
If anyone else needs it, the code is as follows:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1446)->current_query();
$array_data = $sf_current_query->get_array();
$typeOfSearch = $array_data["_sft_pwb-brand"]["active_terms"][0]["value"];
echo $typeOfSearch ;
?>
Where _sft_pwb-brand is the field you need, and 1446 is the ID of the search form.
Are you trying to get terms from the results array? This search shows other user posts that fetch data from the array:
https://support.searchandfilter.com/forums/search/sf_current_query+active_terms/
AnonymousInactive
Hi,
Have an issue trying to extract post types via $postTypes1 = $sf_current_query->get_fields_html(array(“_sf_s”, “_sft_access_level”, “post_type”,”_sft_topic”));
echo $postTypes1;
only returns
Access Levels: All Access Levels (ie: _sft_access_level)
Topics: All Topics (ie: _sft_topic)
but no post types aka Data Type.
You can see the page results here:

The debug structure of the query which clearly shows them here:
Please see image link 
or

Is this a bug with get_fields_html?
If the Data Types are checked as filters it’s possible to retrieve them but that is different from retrieving the associated post types via get fields.
<?php
$postTypes1 = $sf_current_query->get_fields_html(array("_sf_s", "_sft_access_level", "post_type","_sft_topic"));
echo $postTypes1;
//echo $sf_current_query->is_filtered();
$postTypesAll = $sf_current_query->get_array();
//var_dump($postTypesAll);
//print_r(array_values($postTypesAll));
$postTypes = array_column($postTypesAll, 'active_terms');
//var_dump($postTypes);
//print_r(array_values($postTypes));
$newArray = array();
foreach ($postTypes as $entry) {
$names = array_column($entry, 'name');
}
//var_dump($names);
//print_r(array_values($names));
$dataTypes = "<br>Data Types: ";
$breakChars = ", ";
//echo $dataTypes;
for ($i = 0; $i < count($names); $i++) {
$nameCompare = strcmp($names[$i], "Posts");
if ($nameCompare == 0) {
$names[$i] = 'Articles';
}
//echo $names[$i];
if ($i < count($names) - 1) {
echo $breakChars;
}
}
//var_dump($names);
//print_r(array_values($names));
?>
AnonymousInactive
Thanks this code is doing the job for me:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(3301)->current_query()->get_array();
foreach ($sf_current_query as $key => $value) {
$type = $value['type'];
$valuei = $value['active_terms'][0]['value'];
$valuei1 = $value['active_terms'][1]['value'];
$name = $value['active_terms'][0]['name'];
$name1 = $value['active_terms'][1]['name'];
if ($key == '_sft_category') {
if ($name1 == NULL) {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo $name;
echo '</button>';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo $name;
echo '</button>';
echo '<button id="'.$key.'" class="but'.$type.'">';
echo $name1;
echo '</button>';
}
}
if ($key == '_sft_materiaalsoorten') {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo $name;
echo '</button>';
}
if ($key == '_sfm_prijsklasse') {
if ($valuei == 0) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Prijsklasse: ';
echo $name;
echo ' vanaf';
echo '</button>';
}
if ($valuei1 == 50000) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Prijsklasse: ';
echo $name1;
echo ' tot';
echo '</button>';
}
}
if ($key == '_sfm_vrije_valhoogte') {
if ($valuei == 0) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Valhoogte: ';
echo $name;
echo ' vanaf';
echo '</button>';
}
if ($valuei1 == 300) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Valhoogte: ';
echo $name1;
echo ' tot';
echo '</button>';
}
}
if ($key == '_sfm_beschikbare_obstakelvrije_ruimte') {
if ($valuei == 0) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Beschikbare obstakelvrije ruimte: ';
echo $name;
echo ' vanaf';
echo '</button>';
}
if ($valuei1 == 350) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Beschikbare obstakelvrije ruimte: ';
echo $name1;
echo ' tot';
echo '</button>';
}
}
if ($key == '_sfm_leeftijdsindicatie_van') {
if ($valuei == 0) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Leeftijdsindicatie: vanaf ';
echo $name;
echo '</button>';
}
if ($valuei1 == 20) {
echo '';
} else {
echo '<button id="'.$key.'" class="but'.$type.'">';
echo 'Leeftijdsindicatie: tot ';
echo $name1;
echo '</button>';
}
}
}
?>
But what now? How can I make it when somebody clicks on a button the filter turns off?
AnonymousInactive
Thanks Trevor. I’m pretty new to PHP so I hope you dont mind me asking questions.
I got this code:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(3301)->current_query()->get_array();
foreach($sf_current_query as $key) {
$type = $key['type'];
$name = $key['active_terms'][0]['name'];
$name1 = $key['active_terms'][1]['name'];
echo '<button class="' . $type . '">';
echo $name;
if ($name1 == NULL) {
} else {
echo ' - ';
echo $name1;
}
echo '</button>';
}
I want the id of the button to be the array name.
Like: <button id="_sfm_prijsklasse" class="post_meta">
<button id="_sft_category" class="taxonomy">
How can I do this?
AnonymousInactive
Unfortunately that’s a no-go. The complete code I put in (using your good chunk there) looked like:
<?php global $searchandfilter;
$sf_current_query = $searchandfilter->get(15)->current_query()->get_array();
echo '<strong>Filtered by:</strong>';
if ( is_empty($sf_current_query ) ) {
echo "all";
} else {
foreach($sf_current_query as $key) {
echo ' • ' . $key['active_terms'][0]['name'] . ' ';
}
}?>
Am I missing something above if? Feels like that’s maybe where I’m off?
(The page does not display anything – no sidebar, no search form, so as-is above it pretty broken – did I close off the function properly?)
AnonymousInactive
Hello! A while back I had a question about displaying the terms that a user had chosen to filter their search by, and we’re using this:
<?php global $searchandfilter;
$sf_current_query = $searchandfilter->get(15)->current_query()->get_array();
echo '<strong>Filtered by:</strong>';
foreach($sf_current_query as $key) {
echo ' • ' . $key['active_terms'][0]['name'] . ' ';
}
?>
Which works perfectly except that it’s blank when no terms have been selected, so the user sees “Filtered by:” and nothing else.
Can I add to the php above, using an if / else combo?
So:
if $sf_current_query is blank {
echo 'all';
else foreach($sf_current_query as $key) {
echo ' • ' . $key['active_terms'][0]['name'] . ' ';
} ?>
The “if” bit is where I’m lost – please help!