AnonymousInactive
Hello,
So I have 2 S&F forms on my pages (different results pages). Is it possible to pass the query from my main text search to a sidebar filters S&F form I have? The filters on the left live on results page from the main search bar, and if you try to use them they reset the query from the text search (which makes sense technically). I am wondering if there is a way that if a user hits ones of those filters I can pass the query from the other S&F form?
I saw this:
function filter_function_name( $query_args, $sfid ) {
if ( is_search() ) {
//var_dump($sf_current_query);
}
//if search form ID = 225, the do something with this query
if($sfid==225)
{
//modify $query_args here before returning it
$query_args[‘somearg’] = ‘newvalue’;
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );
But wasn’t sure if that was the right path to grab the current query and pass it along to the other form.
Thank you!
The PHP to use to conditionally run code looks like this (change the ID of the form to suit):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
// the current (unfiltered) posts output code here
} else {
// the desired (filtered) posts output code here
}
AnonymousInactive
Trevor,
It works.
$sf_current_query = $searchandfilter->get($id)->current_query();
$term = $sf_current_query->get_search_term();
$count = $wp_query->found_posts;
Thank you,
Christian
You will not find the count in the $sf_current_query array.
What display results method are you using?
AnonymousInactive
Trevor,
I did try this:
$sf_current_query = $searchandfilter->get($id)->current_query();
$term = $sf_current_query->get_search_term();
$count = $sf_current_query->found_posts;
I returns the term, but not the count. How can I get the results count from the Search and Filter query?
Thank you,
Christian
AnonymousInactive
Hi Trevor,
I wish to display the number of results generated by a query on the search results page. I found how to get the search term in your documentation.
$sf_current_query = $searchandfilter->get($id)->current_query();
$term = $sf_current_query->get_search_term();
How do I get the number of results?
Thank you,
Christian
Are you using our Shortcode Display Results method. If not, you would need to be using a method that uses a (simple) theme PHP template and be abel to add code like this (the ID number needs to match your form):
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 (filtered) posts output code here
}
So, the logic would look something like this (the ID would match that of the search form):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
// the code to display the 8-10 default houses
} else {
// the normal results loop code here
}
But, I am not sure Avada uses traditional templates, so this may be difficult to code. In the past, other Avada users have needed to use a third party grid plugin, such as this method (which does not offer the option to do as you want in this case):
https://searchandfilter.com/documentation/3rd-party/post-grid/
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.
AnonymousInactive
Just FYI, I have added this code within my #main container on the archive template. Nothing shows.
global $searchandfilter;
$sf_current_query = $searchandfilter->get(9490)->current_query();
echo $sf_current_query->get_field_string("_sfm_UnitLocation");