AnonymousInactive
Continued from https://support.searchandfilter.com/forums/topic/hide-initial-results-on-first-page-load/
Sorry, that last thread was closed but I’ve discovered a problem.
The if/else check at the beginning is failing if I’m filtering for a category but the search text filed is empty.
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
echo '<div> </div>';
} else
AnonymousInactive
Hi again 🙂
What does the 1726 in the 2nd line of that code refer to? Is that a page ID?
$sf_current_query = $searchandfilter->get(1726)->current_query();
Do you know if you have the name correct? You could use PHP to dump the $sf_current_query array, like this:
echo '<pre>';
print_r($sf_current_query);
echo '</pre>';
AnonymousInactive
https://domainnamehere.com/resources/?_sft_resourcetype=press-releases&post_date=01012016+12312016
I’ve had success getting the query variable for the taxonomy term “press-releases” using the code below, but cannot successfully retrieve the date range, either aggregated or individually as from date and to date. Can you enlighten me?
global $searchandfilter;
$sf_current_query = $searchandfilter->get(5277)->current_query();
$type = $sf_current_query->get_field_string("_sft_resourcetype"); //returns press-releases
$daterange = $sf_current_query->get_field_string("post_date"); // returns nothing
Have also tried:
$daterange = $sf_current_query->get_field_string("_sfm_post_date"); // returns nothing, even with url reflecting _sfm_ prefix
OK. The >> is made by your theme and needs to be switched off inside search and filter forms, with Custom CSS:
.bSe .searchandfilter ul li::before {content: '' !important;}
To put the search field and button next to each other, this:
.searchandfilter .sf-field-search {
display: inline-block;
padding-left: 0 !important;
margin-right: 20px;
}
.searchandfilter .sf-field-submit {
display: inline-block;
padding-left: 0 !important;
}
For the second point, to hide results until a search has been made, I need to know you have placed a copy of the results.php file in your theme or child theme folder, as here:
https://www.designsandcode.com/documentation/search-filter-pro/search-results/using-a-shortcode/#Customising_the_Results
Assuming that you have, edit that file and replace the content with 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
*
*/
global $searchandfilter;
$sf_current_query = $searchandfilter->get(255)->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";
}
}
?>
That should work, I hope.
For #3. It would be better to use your theme’s own styling, and to do this I would need to see a more normal results page in your theme that looks OK to you, then we can borrow their styling.
Hey Kevin
Sorted it!
Basically what you had done was correct… is_filtered returns true if a filter has been selected.
This is not true for the search term though, as its not regarded as a filter (perhaps it should be now that I think about it)…
So what I did was also add a condition to see if a user had typed a search term to your template, and now it works as expected –
$sf_current_query->get_search_term()==""
Best
Something like 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
*
*/
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1726)->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";
}
}
?>
I think?
AnonymousInactive
[thread split from https://support.searchandfilter.com/forums/topic/how-to-not-show-results-in-first-search-translate-older-newer-posts/ by @trevorsf ]
Hi Trevor,
I’ve attempted to use this code (and the solutions from the other threads without any luck at all). I don’t want to start another thread with the same question because you’ve had to repeat yourself a few times. Hopefully this is OK.
I’m using the shortcode method for displaying my search and results. I tried the second piece of code from this thread: https://support.searchandfilter.com/forums/topic/how-do-i-hide-initial-results/page/2/#post-44951 but it displays all the items on load instead of hiding them until someone searches.
In this example you have a ! in “if (!$sf_current_query->is_filtered()) {” and say nothing should go in the brackets before the else statement. I’ve added the output part of the results.php but I’m not have any success.
Would it be possible to get an updated results.php example like you did in the other thread that will display nothing until the user searches and if they get no results it will display the message?
AnonymousInactive
On your documentation
https://www.designsandcode.com/documentation/search-filter-pro/accessing-search-data/
you say this is possible querying the form from another page:
<?php
//grab the active query from our search form
//replace <code>1526</code> with the ID of your search form
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1526)->current_query();
?>
Whould this display the filtered posts to a php template?
Hi
For 1 and 2, you would need to edit the results.php file that you copied into your theme folder. 3 is not possible, yet. It is on our roadmap, but it may be a while yet, to allow form elements in different places on the page.
So, for 1. This is the basic code:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1726)->current_query();
if (!$sf_current_query->is_filtered()) {
// put anything you want instead of the results for the page loading without query parameter
} else {
// put the output part of the results.php here
}
Where 1726 is changed to whatever the ID number of your form is.
For 2, simply find those text strings in the results.php file and replace.