The forums are closed and will be removed when we launch our new site.
Looking for support? You can access the support system via your account.
Forums › Forums › Search & Filter Pro › Hide Posts if nothing is selected, then show them
- This topic has 28 replies, 2 voices, and was last updated 4 years, 8 months ago by
Trevor.
-
Javier Martín Balsa(Private) January 21, 2020 at 3:40 pm #231704
Hi there I open a ticket a few weeks ago:
But I`ve got a problem, this is my link:
http://www2.vamos.es/renting-de-coches
And this is my code:
<div class="col-sm-3 todos-coches-filter hidden-xs"> <?php echo do_shortcode ('[searchandfilter id="5555"]'); ?> </div> <div class="col-sm-9 resultados" data-wow-duration="1s" data-wow-delay="0.3s"> <?php $args = array('post_type' => 'renting'); $args['search_filter_id'] = 5555; $query = new WP_Query($args);?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); // run the loop ?> <?php global $searchandfilter; $sf_current_query = $searchandfilter->get(5555)->current_query(); if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { ?> <?php // set up or arguments for our custom query $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $query_args = array( 'post_type' => 'renting', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'mostrar-en-parrilla', 'field' => 'slug', 'terms' => 'si', ), ), 'posts_per_page' => 30, 'paged' => $paged ); // create a new instance of WP_Query $the_query = new WP_Query( $query_args ); ?> <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?> <?php the_title (''); ?> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?> <?php } else { ?> <?php the_title (''); ?> <?php } ?> <?php endwhile; else: ?> <div class="no-results">No hemos encontrado resultados, prueba con otros criterios de filtro.</div> <?php endif; ?> <?php wp_reset_query(); ?> </div>
I’ve got an eternal loop of cars if nothing is selected in the filter, when you use the filter everything is ok.
Can u help me here?
Thanks
Trevor Moderator(Private) January 21, 2020 at 3:57 pm #231711What did the template code look like BEFORE you edited it?
Javier Martín Balsa(Private) January 21, 2020 at 4:00 pm #231715This is a page that I’m using for the filter, but I needed to have results before the user uses the filter, that’s why I opened the other post asking for help.
This is the code I’ve got before:
<?php echo do_shortcode ('[searchandfilter id="2611"]'); ?> <div class="filtro-cars" data-wow-duration="1s" data-wow-delay="0.3s"> <?php global $searchandfilter; $sf_current_query = $searchandfilter->get(2611)->current_query(); if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { echo ''; } else { ?> <?php $args = array('post_type' => 'renting'); $args['search_filter_id'] = 2611; $query = new WP_Query($args);?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); // run the loop ?> <?php the_title (''); ?> <?php endwhile; else: ?> <div class="no-results">No hemos encontrado resultados, prueba con otros criterios de búsqueda.</div> <?php endif; ?> <?php wp_reset_query(); ?> <?php } ?> </div>
Trevor Moderator(Private) January 21, 2020 at 4:08 pm #231721Sorry, I did not make it clear what I needed. Assume our filter is not in use, so before you added any code, what did this template look like? Before you added the form shortcode and before you added the code I gave you?
The problem is that I can see the loop starting twice, but not ending. So I need to see the basic page without the influence of our plugin. Going back in time.
Javier Martín Balsa(Private) January 21, 2020 at 4:16 pm #231727I tried to modify the filter i’ve got working on my home page but with results. On my home the filter hides any results before the user select an option and press the submit button.
and the code was the one I gave you before:
<?php echo do_shortcode ('[searchandfilter id="2611"]'); ?> <div class="filtro-cars" data-wow-duration="1s" data-wow-delay="0.3s"> <?php global $searchandfilter; $sf_current_query = $searchandfilter->get(2611)->current_query(); if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { echo ''; } else { ?> <?php $args = array('post_type' => 'renting'); $args['search_filter_id'] = 2611; $query = new WP_Query($args);?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); // run the loop ?> <?php the_title (''); ?> <?php endwhile; else: ?> <div class="no-results">No hemos encontrado resultados, prueba con otros criterios de búsqueda.</div> <?php endif; ?> <?php wp_reset_query(); ?> <?php } ?> </div>
Maybe I need to start all over again, I need a page with all the results of a query of custom post (excluding some posts by ID) and then when the user use the filter the results must show by ajax.
🙂
Javier Martín Balsa(Private) January 21, 2020 at 4:25 pm #231729I can give you access to my FTP development site.
Trevor Moderator(Private) January 21, 2020 at 4:44 pm #231737Would that have the original file before you edited it? All I need is that original file before edits.
Javier Martín Balsa(Private) January 21, 2020 at 4:49 pm #231739There was nothing on this php file, is custom made, I started from scratch….
Trevor Moderator(Private) January 21, 2020 at 4:58 pm #231741Did you use other templates as a basis for example code? The problem I face is that the code you showed me does not have a good structure.
At the least it should look like this, with no wp_query or arguments inside this code, they should go first:
`<?php
// arguments and wp_query here
global $searchandfilter;
$sf_current_query = $searchandfilter->get(2611)->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() )
{
?><?php
while ($query->have_posts())
{
$query->the_post();
// output the post here
}}
else
{
echo “<span id=’no-results’>No results found, please try altering your search criteria.</span>”;
}
}
?>But, for example, you have wp_query being run twice, two sets of arguments, and starting if have_posts twice?
Javier Martín Balsa(Private) January 21, 2020 at 5:11 pm #231743I can’t make it work properly.
I need this to happen:
1- Results are shown when the page loads with a custom query to hide post by ID
2- When the filter is used then displays by ajax the results including the post hidden before.That’s all
Is it possible?? Please help, I’m not so pro at coding.
Thanks
-
AuthorPosts
The topic ‘Hide Posts if nothing is selected, then show them’ is closed to new replies.