You would need to dig deeper in the $sf_current_query
array I suspect. You can echo the array to a page with a PHP print_r function inside pre tags.
AnonymousInactive
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(504)->current_query();
echo $sf_current_query->get_field_string("_sft_category");
?>
The above code gets the current category, but how to do it without the extraneous word(s) before it?
Thanks
Hi
You could use PHP to see if the results are filtered, like this (change the ID number for yours):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query();
if (($sf_current_query->is_filtered())||($sf_current_query->get_search_term()!="")) {
// do something that identifies a search has been made (set some CSS, ...)
}
// your normal template code here
AnonymousInactive
Hey!
So the post “Customize initial results” was really helpful, however is it possible to display a random list of results where “// your holding template code here” is at?
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
// your holding template code here
} else {
// your normal template code here
}
Thanks!
If you have access to the PHP template files, you can use something like this (change the ID for your form ID):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
// your holding template code here
} else {
// your normal template code here
}
With the shortcode method, the URL re-write is not possible (it is possible only with The Post Type Archive/WooCommerce Display Results Method). This is a feature planed for V3 (being written now, release later in 2017).
You can access the current query by adding code to the results.php file.
I am unable to code this for you, but if you search the forum for this string sf_current_query
, you will find some examples.
AnonymousInactive
Wooo, that got it. Thank you for your patience.
For anyone else with this issue please see my code below. Probably not perfect but it works. 🙂
// Exclude the default range values from the search as they are meaning the search returns no results
function remove_default_range( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==25 && $query_args) {
if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) {
global $searchandfilter;
$sf_current_query = $searchandfilter->get(25)->current_query()->get_array();
/* echo "<pre>";
print_r($sf_current_query);
echo "</pre>";*/
echo $sf_current_query['_sfm_minimum_per_round'][0]['value'];
if($sf_current_query['_sfm_minimum_per_round'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_round'][0]['value'] == '50000') {
unset($sf_current_query['_sfm_minimum_per_round']);
}
if($sf_current_query['_sfm_minimum_per_season'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_season'][0]['value'] == '1000000') {
unset($sf_current_query['_sfm_minimum_per_season']);
}
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'remove_default_range', 20, 2 );
It is a while since I looked at the array, but I think I did something like this:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(25)->current_query()->get_array();
echo "<pre>";
print_r($sf_current_query);
echo "</pre>";
This is not so easy, as it would the use of PHP around that grid, and I do not think VC can do that. This is how that is done in PHP:
You can show them nothing (or a custom message) using this type of PHP code (change the number 1234 for the ID number of your search form and change the line echo ‘<div>Nothing to see here folks!</div>’; for whatever you want before the filter is used instead of the results – or remove for nothing at all):
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 {
// your VC Grid here
}
As I say, I am not sure how you can do that though.
AnonymousInactive
I found some workaround, to display currently selected value:
$tmp = $sf_current_query->get_array();
echo $tmp["_sfm_character"]["active_terms"][0]["name"];
But it may be a bug in get_field_string
method from documentation, which is not working in my case:
echo $sf_current_query->get_field_string("_sfm_character");