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?
_sft_meta_value, would, I think, be _sfm_county
So, you would echo something like this:
echo $sf_current_query->get_fields_html(array("_sft_category"));
echo $sf_current_query["_sfm_county"]["active_terms"][0]["value"];
With County, that code assumes you have just one term selected. If none are selected, you might have first to test if that has a value. Like this:
echo $sf_current_query->get_fields_html(array("_sft_category"));
$county = $sf_current_query["_sfm_county"]["active_terms"][0]["value"];
if ($county) {
echo $county;
} else {
echo "All Counties";
}
If you allow more than one county, it would get a little more complicated, but not much.
AnonymousInactive
On NovaScotiaFood.com I have a search form that uses AND as an operator. The user simply selects a category of food producer and a county, and they get a list of that type of food producer in the selected county. The search works fine. The results are not yet displaying the way I’d like.
I’ve just copied some of your example code on my results page:
<?php
//Get a multiple fields values by passing an array of field names
//replace <code>1526</code> with the ID of your search form
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1949)->current_query();
/*
* EXAMPLE 1
*/
echo $sf_current_query->get_fields_html(
array("_sft_category", "_sft_meta_value")
);
where I just tried to guess that ‘_sft_meta_value’ would output the custom field entry that was searched for, but I guessed wrong. What should I be putting in there instead to correctly output the custom field? Thanks.
You have chosen to use for now our Shortcode display results method, which means that the style of the display is somewhat basic, but can be customized by yourself:
https://searchandfilter.com/documentation/search-results/using-a-shortcode/#customising-the-results
It depends what content you want (what taxonomies, images, description, custom fields) and layout you want, as that will determine how easy it will be to do.
#1 This code:
Found <?php echo $query->found_posts; ?> Results<br />
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
Would become this:
<div style="text-align: right">Found <?php echo $query->found_posts; ?> Results, Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?></div>
#2 The above code would then become:
<div style="text-align: right">
<?php global $searchandfilter;
$sf_current_query = $searchandfilter->get(13622)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { } else { ?>
Found <?php echo $query->found_posts; ?> Results,
<?php } ?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?></div>
#3 This space?
https://www.screencast.com/t/GuYOFQ3qtQ3l
There appears to be blank lines in your content:
https://www.screencast.com/t/SivDBdT69fK
#4 This would require our filter to use (or be able to use) transients, which it cannot at present. I believe this will be added when we release V3, towards the end of June/early July.
Even then, you would probably require the services of a coder to hook it all up.
I am assuming that you are using the WooCommerce method? You could edit the PHP template being used and add some logic to test if a filter has yet been applied, like this (change the ID to match the form you are using):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>Nothing to see here folks!</div>';
} else {
// the current products output code here
}
You need to modify the code in the results.php to this:
<?php
/**
* Search & Filter Pro
*
* Sample Results Template
*
* @package Search_Filter
* @author Ross Morsali
* @link http://www.designsandcode.com/
* @copyright 2015 Designs & Code
*
* 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;
}
global $searchandfilter;
$sf_current_query = $searchandfilter->get(70)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>Nothing to see here folks!</div>';
} else {
if ( $query->have_posts() )
{
?>
Found <?php echo $query->found_posts; ?> Results<br />
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( 'Older posts', $query->max_num_pages ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php
/* example code for using the wp_pagenavi plugin */
if (function_exists('wp_pagenavi'))
{
echo "<br />";
wp_pagenavi( array( 'query' => $query ) );
}
?>
</div>
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><br /><?php the_excerpt(); ?></p>
<?php
if ( has_post_thumbnail() ) {
echo '<p>';
the_post_thumbnail("small");
echo '</p>';
}
?>
<p><?php the_category(); ?></p>
<p><?php the_tags(); ?></p>
<p><small><?php the_date(); ?></small></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( 'Older posts', $query->max_num_pages ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></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";
}
}
?>
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.