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']
In the form setting, which Display Results method are you using?
From your question, I assume that you are not using our Shortcode Display results method. If you are using some form of page builder, what you want will not currently be possible (but we hope to make it so in V3, due in a few months).
If you are using the As and Archive, Post Type Archive or some of the methods that use the Custom method, and if you have access to a PHP template that is used in the display of the page/results, then it is, with good PHP skills, to code what you want. You would need to know what the results array variable is named if you are using a theme PHP template. It is often named $query or $wp_query, but can be anything the developer of the theme wants.
For example, to display the number of results, code LIKE this would do that:
Found <?php echo $query->found_posts; ?> Results
Code LIKE this (change the ID number to be that of your form) would display the text search term, for example:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(12345)->current_query();
if (!$sf_current_query->get_search_term()=="") {
echo '<div>Displaying: ' . $sf_current_query->get_search_term() . '</div>';
}
You would want these code snippets placed inside the block that is being refreshed with Ajax (the ‘Ajax Container’).
AnonymousInactive
My S&F is entirely filtering on ACF fields as post meta results.
I’d like to display the selected search terms, however there seems to be a difference in how to get these vs more standard fields like category.
For example:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1526)->current_query();
echo $sf_current_query->get_field_string("_sft_category");
gives the expected result, but changing the argument in the third line to be a custom field using the name that appears in the url bar(?_sfm_work_type=26-%2C-27&_sfm_industries=14-%2C-15) doesn’t give any results, even though the filter works fine.
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1526)->current_query();
echo $sf_current_query->get_field_string("_sfm_industries");
Is there a change in method to get those fields?
It would require custom coding in the template for the page, around the part that shows the results loop.
Something like this in PHP (this example assumes you are using our Shortcode method, which in turn uses a template named results.php):
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
}
The ID number must match that of your form. My message line would simply not be there at all.
Note, if you are using a page builder, this method may not be possible.
Whoops:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(102003)->current_query();
if (!$sf_current_query->get_search_term()=="") {
echo '<div>' . $sf_current_query->get_search_term() . '</div>';
}
Ah. What about this (I did get the form ID number right, yes?):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(102003)->current_query();
if (!$sf_current_query->get_search_term()=="") {
echo '<div>$sf_current_query->get_search_term()</div>';
}
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.
AnonymousInactive
Hello,
We have set up a page with S&F with the display results method ‘Using a shortcode’.
Before the shortcode is executed, I would like to get the first result of the query (I want to display the first item as a highlight before the complete results).
I thought I could get the results via:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(100773)->current_query();
But it seems there are no results in this object.
Is there a way to retrieve the first item of a query from the shortcode method?
Thanks in advance!
-Kevin
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";
}
?>