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?
AnonymousInactive
Hi, I am using your (terrific) plugin with Elementor Pro. I am having quite a hard time displaying a headline with the chosen custom categories above my search results. I mean: Search results for taxonomy a1, taxonomy a2, taxonomy b and taxonomy c.
I found your post:
https://support.searchandfilter.com/forums/topic/results-displayed-results-count-searched-tag-associated-categories/
and adapted it for my use. It turned out quite well BUT this headline does not change when I select another filter. Can you think of any solution for this?
My code:
<?php
global $searchandfilter;
$sf_query = $searchandfilter->get(5360)->current_query();
if ($sf_query->is_filtered()) {
$sf_current_query = $searchandfilter->get(5360)->current_query()->get_array();
echo "<b>Onderzoek met betrekking tot ";
$cat_string = "";
foreach($sf_current_query['_sft_category']['active_terms'] as $item) {
$cat_string .= $item['name'] . ", ";
}
$starost_string = "";
foreach($sf_current_query['_sfm_starost']['active_terms'] as $item) {
$starost_string .= $item['name'] . ", ";
}
if (($sf_current_query['_sft_reflex-categorie']['active_terms'][0]['name'])<>"") echo "<p>" . ($sf_current_query['_sft_reflex-categorie']['active_terms'][0]['name']) . "</p>";
if (($sf_current_query['_sft_vaardigheden-categorie']['active_terms'][0]['name'])<>"") echo "<p>" . ($sf_current_query['_sft_vaardigheden-categorie']['active_terms'][0]['name']) . "</p>";
if (($sf_current_query['_sft_diagnose-categorie']['active_terms'][0]['name'])<>"") echo "<p>" . ($sf_current_query['_sft_diagnose-categorie']['active_terms'][0]['name']) . "</p>";
echo "</b>";
}
?>
My CSS:
p {
margin: 0;
display: inline;
}
p:not(:last-child)::after { content: ” & “; }
p:last-child::after { content: “”; }
Thank you so much in advance!
Monique
So, here is a search:
_sft_location=cotswolds
_sft_partysize=ten-guests
_sft_features=garden-patio,internet
The code I added was this (replace the 12345 with your form ID):
<div>
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(12345)->current_query()->get_array();
$locations_filter = $sf_current_query["_sft_location"]["active_terms"][0]["name"];
if ( $locations_filter <> "") {
echo '<div>Location: ' . $locations_filter . '</div>';
}
$party_size_filter = $sf_current_query["_sft_partysize"]["active_terms"][0]["name"];
if ( $party_size_filter <> "") {
echo '<div>Guests: ' . $party_size_filter . '</div>';
}
$features_filter_array = $sf_current_query["_sft_features"]["active_terms"];
if ( $features_filter_array ) {
echo '<div>Features: ';
foreach($features_filter_array as $value) {
$features_list .= $value["name"] . ", ";
}
echo rtrim($features_list, ", ") . '</div>';
}
?>
</div>
Hi Sigurd
After scratching my head I managed to figure out the issue.
We perform our search outside of the regular WP_Query, so if you choose a category in our search form, WP doesn’t actually “know” its restrciting to a specific category, because our search does that, so WP appends the stick post…
I’ve managed to come up with some code, that add the WP category to the WP_Query (even though our plugin has already restricted the posts to this category), but now WP also knows that at a category restriction is happening, and excludes the sticky:
add_filter("sf_edit_query_args", "add_wp_category_to_query", 100, 2);
function add_wp_category_to_query($query_args, $sfid){
//if($sfid == 1234){ //you can restrict this behaviour to a search form ID
//get user category selection from the search form (ie, detect which categories have been selected)
global $searchandfilter;
$query_data = $searchandfilter->get($sfid)->current_query();
$query_data_array = $query_data->get_array();
if(isset($query_data_array['_sft_category'])){
$category_values = $query_data_array['_sft_category']['active_terms'];
$current_category_ids = array(); //this will contain all the IDs of the categories that have been selected in the search form
foreach($category_values as $category){
array_push($current_category_ids, $category['id']);
}
//restrict query to WP category
$query_args['category__in'] = $current_category_ids;
}
//}
return $query_args;
}
As there may be some small overhead when implementing this code, you can uncomment the first if( line, and restrict this to a specific search form / query.
I hope that makes sense & works!
Best
You seem to have customised the results.php file, but it must have errors as it also shows ‘no results found’?
This post might help you with code for what you want:
https://support.searchandfilter.com/forums/topic/category-description-in-search-results/page/2/#post-236248
That code is for _sft_category and the id number is wrong, so maybe this?
global $searchandfilter;
$sf_current_query = $searchandfilter->get(3637)->current_query();
$sf_current_query_array = $searchandfilter->get(3637)->current_query()->get_array();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>No category selected.</div>';
} else {
echo '<div>' . $sf_current_query_array["_sft_menucats"]["active_terms"][0]["value"] . '</div>';
echo '<div>' . $sf_current_query_array["_sft_menucats"]["active_terms"][0]["id"]. '</div>';
echo '<div>' . category_description($sf_current_query_array["_sft_menucats"]["active_terms"][0]["id"]). '</div>';
}
Otherwise, are you able to send me temporary WordPress admin logins (ones you should later delete or disable) for the site so that I can look at the setup? Please use a Private Reply.
AnonymousInactive
I tried to unset the slider using sf_edit_query_args, but it doesn’t clear the search results. It still keeps the slider query!
This is what I have tried:
function filter_function_name( $query_args, $sfid ) {
global $searchandfilter;
$current_query = $searchandfilter->get(58376)->current_query()->get_array();
unset( $current_query['_sfm_LENGTH_KEY'] );
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
But it doesn’t do anything! Posts are still being filtered with the slider query!
I added this code:
https://www.screencast.com/t/WYIjHhwBYb
This:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(332)->current_query();
$sf_current_query_array = $searchandfilter->get(332)->current_query()->get_array();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>No category selected.</div>';
} else {
echo '<div>' . $sf_current_query_array["_sft_category"]["active_terms"][0]["value"] . '</div>';
echo '<div>' . $sf_current_query_array["_sft_category"]["active_terms"][0]["id"]. '</div>';
echo '<div>' . category_description($sf_current_query_array["_sft_category"]["active_terms"][0]["id"]). '</div>';
}
If you use this code you would get the whole array:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1902)->current_query()->get_array();
echo '<pre>',print_r($sf_current_query,true),'</pre>';
?>
Then you can see what each is called and contains.
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.
You could temporarily use this code in your results template to display the full filter array so that you can work out the exact array part that holds the data you want:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1391)->current_query()->get_array();
echo '<pre>',print_r($sf_current_query,true),'</pre>';
?>
Where the ID needs to match that of your form.
AnonymousInactive
I have a need to refresh the custom results page using an html form that controls how results are displayed, (map VS list). When the page gets called by itself, (form action = call this page), setting the WP_query $args, (in my case… $args = array(‘search_filter_id’ => 1576,’post_type’ => ‘post’,’paged’ => $paged); ), doesn’t work since the value for ‘search_filter_id’ => 1576 is NULL when the page gets refreshed. I’ve tried a few ways to contruct a new $args value that I set in a $_SESSION variable but can’t get it to work. Here’s what I’ve tried:
Executed on initial page display—Works
1. $sf_current_query = $searchandfilter->get(1576)->current_query();
2. $original_sf_query_args =array($sf_current_query->get_array());
3. $passed_args_array = array($original_sf_query_args,’post_type’ => ‘post’,’paged’ => $paged);
4. $_SESSION[‘passed_args_array’] = $passed_args_array;
5. $args = array(‘search_filter_id’ => 1576,’post_type’ => ‘post’,’paged’ => $paged);
6. $query = new WP_Query($args);
Executed on subsequent page displays—Doesn’t Work
1. $args = $_SESSION[‘passed_args_array’];
2. $query = new WP_Query($args);
What might I be doing wrong?