If you are using the Shortcode results method, and our results.php template file, it would look like this:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1024)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>Nothing to see here folks!</div>';
} else {
// your template archive code/loop OR results.php code here
}
Change 1024 to the ID number of your form (from the form shortcode).
You would need to be using our Shortcode Display results method, and have followed the guide to customising:
https://support.searchandfilter.com/documentation/search-filter-pro/search-results/using-a-shortcode/
Once you have a copy of the results.php file in a search-filter
sub-folder of your theme, you can edit that file, like this (leave the PHP comments at the top outside and before this code):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1024)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>Nothing to see here folks!</div>';
} else {
// the results.php PHP code here
}
Make sure you change the ID number in that snippet to the ID number of your form.
The Custom method relies on a theme template of some sort. Are you using a theme php template, as the logic would look something like this to do as you want:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1024)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>Nothing to see here folks!</div>';
} else {
// the theme template PHP code here
}
Change the number 1024 with your form’s ID number (from the shortcode).
This code will likely need to be used within a part the template file that actually outputs the results (around the if query … have … posts bit).
You could use PHP to look at the array that holds the filter arguments, and conditionally load different code in the same template. Again, that is custom coding.
To start with, put this code on the results page in a suitable place:
$sf_current_query_array = $searchandfilter->get(1446)->current_query()->get_array();
echo '<pre>';
print_r($sf_current_query_array);
echo '</pre>';
CHANGE THE ID NUMBER in the above code (1446) to whatever the ID of you form is.
You will then see the array of filters used, and you can figure than how to fetch the value you need to add PHP to your template to test what the user has selected.
Ah, that will be easier to do in V3 of our plugin, but for now it would have to be custom coded. This documentation page is a starting point:
https://searchandfilter.com/documentation/accessing-search-data/
Many users have posted more complex solutions, and this search should pick up some of those threads:
https://support.searchandfilter.com/forums/search/current_query%28%29+get_array%28%29/
AnonymousInactive
How can I set up a custom message when search returns 0 results?
Site: Click here
My code:
<?php
global $searchandfilter;
$atrair_home = $searchandfilter->get(180)->current_query();
if ((!$atrair_home->is_filtered())&&($atrair_home->get_search_term()=="")) {
$args = array(
'post_type' => array(
'post',
'ebooks',
'podcasts',
'videos'
),
'post_status' => 'publish',
);
$posts = new WP_Query( $args );
if( $posts->have_posts() ) :
?>
<?php
while( $posts->have_posts() ) :
$posts->the_post();
?>
<div class="posts-double">
<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
<a class="thumbnail" href="<?php the_permalink() ?>" rel="bookmark">
<?php
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'thumbnail');
echo '<div class="image" style="background:url('.esc_url($featured_img_url).')"></div>' ?>
<?php endif; ?>
</a>
<div class="content">
<a class="title" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
<p class="descriptions">
<?php echo wp_trim_words( get_the_content(), 15, '...' );?>
</p>
<div class="related-post-category">
<?php the_tags();
$post_type = get_post_type(get_the_ID());
$post_name = get_post_type_object(get_post_type())->labels;
if ( $post_type != 'post' ) {
echo '<a href="'.get_post_type_archive_link( $post_type ).'"><span class="'.$post_type.'">'.$post_name->name.'</span></a>';
};
?>
</div>
</div>
<div class="posts-footer">
<div class="related-time">
<?php echo do_shortcode('[rt_reading_time]'); ?>
</div>
<div class="related-bookmark">
<?php echo do_shortcode('[cbxwpbookmarkbtn]'); ?>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
else :
echo 'Não conseguimos encontrar nenhum resultado...';
endif;
}
else
//Inicio fim
{
if ( $query->have_posts() )
{
?>
Found <?php echo $query->found_posts; ?> Results<br />
<div class='search-filter-results-list'>
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div class="posts-double">
<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
<a class="thumbnail" href="<?php the_permalink() ?>" rel="bookmark">
<?php
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'thumbnail');
echo '<div class="image" style="background:url('.esc_url($featured_img_url).')"></div>' ?>
<?php endif; ?>
</a>
<div class="content">
<a class="title" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
<p class="descriptions">
<?php echo wp_trim_words( get_the_content(), 15, '...' );?>
</p>
<div class="related-post-category">
<?php the_tags();
$post_type = get_post_type(get_the_ID());
$post_name = get_post_type_object(get_post_type())->labels;
if ( $post_type != 'post' ) {
echo '<a href="'.get_post_type_archive_link( $post_type ).'"><span class="'.$post_type.'">'.$post_name->name.'</span></a>';
};
?>
</div>
</div>
<div class="posts-footer">
<div class="related-time">
<?php echo do_shortcode('[rt_reading_time]'); ?>
</div>
<div class="related-bookmark">
<?php echo do_shortcode('[cbxwpbookmarkbtn]'); ?>
</div>
</div>
</div>
<?php
}
?>
</div>
<?php
}
else
{
?>
<div class='search-filter-results-list' data-search-filter-action='infinite-scroll-end'>
<span>End of Results</span>
</div>
<?php
}
}
?>
AnonymousInactive
Hi Trevor,
Thanks for your help with this. It looks like the code I need to use is:
Get the Search Term
<?php
//Get the search term
//replace 1526
with the ID of your search form
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1526)->current_query();
echo $sf_current_query->get_search_term();
?>
However, I’m not clear where I should be inserting this in the theme – should I be looking for the search.php template file and inserting it in there where the searched terms are shown?
Thanks,
Matthew
You would need to be using our Shortcode Display results method, and have followed the guide to customising:
https://support.searchandfilter.com/documentation/search-filter-pro/search-results/using-a-shortcode/
Once you have a copy of the results.php file in a search-filter
sub-folder of your theme, you can edit that file, like this (leave the PHP comments at the top outside and before this code):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1024)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>Nothing to see here folks!</div>';
} else {
// the results.php PHP code here
}
Make sure you change the ID number in that snippet to the ID number of your form.
Please note that, as your license has expired, you will first need to renew at the (discounted) renewal price to receive support.
Does this code show anything different?
$sf_current_query_array = $searchandfilter->get(1446)->current_query()->get_array();
echo '<pre>';
print_r($sf_current_query_array);
echo '</pre>';
You will need to change the ID number of course. There are some threads on the forum that you can search for where other users have used this code to access the filters data.
With our Shortcode method, in the documentation link I gave you, is the guide to customising.
Once you have a copy of the results.php file in a search-filter
sub-folder of your theme, you can edit that file, like this (leave the PHP comments at the top outside and before this code):
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1024)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div>Nothing to see here folks!</div>';
} else {
// the results.php PHP code here
}