Forums › Forums › Search & Filter Pro › Empty result message
Tagged: empty, filter, No results, search
- This topic has 10 replies, 2 voices, and was last updated 7 years, 7 months ago by Anonymous.
-
Anonymous(Private) March 21, 2017 at 8:50 pm #98514
Hi,
Can I set a message when there are no search results? I am loading my results using ajax.
Some more info. I am using a foreach loop to get the custom terms from a custom post type and within the foreach loop (custom term) I display all custom posts connected to each term.Do I maybe have to set an else statement on the foreach loop or can I do something with ajax to show a message when there are no results?
Best regards,
Joeri.Trevor(Private) March 22, 2017 at 12:55 pm #98590If you are writing your own results template, or modifying one from your theme, you can use PHP to test for the number of results and use an IF statement to conditionally load the content loop or the special message. When you use our Shortcode Results display method, that uses our own template, which you can find in the plugin’s template folder. In that, you can see how we do as I have described, when using the standard WP Query for the loop.
Anonymous(Private) March 28, 2017 at 2:01 pm #99914Hi Trevor,
Thank you for your reply. I don’t know if you can call it an ‘own results template’ because in the settings you set a class, in my case .list, to show the results when you use the filter. I do have my own template for the loop to show the initial posts. Can I show you this template:
<?php $custom_terms = get_terms('alfabet'); if (!empty($custom_terms)) { foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array( 'post_type' => 'bontvrijlijst', 'search_filter_id' => 7242, 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'alfabet', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<div id="' . $custom_term->slug . '" class="alfabet-letter-wrap"><div class="alfabet-letter">'.$custom_term->name.'</div><ul class="members">'; while($loop->have_posts()) : $loop->the_post(); $company_link = get_field('url'); echo '<li class="member"><a href="'. $company_link .'" target="_blank">'.get_the_title().'</a></li>'; endwhile; echo '</ul></div>'; } } } else { echo '<p><strong>Er zijn geen bedrijven die voldoen aan deze zoekopdracht. Probeer het opnieuw.</strong></p>'; } ?>
When there are no results I don’t get the ‘else’ message and in the console I see an error that the main.js file is not found. I don’t know if I can use a link in the reply message but the page is: http://joemarque.nl/dev/bvd/bontvrijlijst-test/.
Maybe you can see what I am doing wrong.
Best regards,
Joeri.Trevor(Private) March 28, 2017 at 2:14 pm #99926See if this fixes the nor results message:
<?php $custom_terms = get_terms('alfabet'); if (!empty($custom_terms)) { foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array( 'post_type' => 'bontvrijlijst', 'search_filter_id' => 7242, 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'alfabet', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<div id="' . $custom_term->slug . '" class="alfabet-letter-wrap"><div class="alfabet-letter">'.$custom_term->name.'</div><ul class="members">'; while($loop->have_posts()) : $loop->the_post(); $company_link = get_field('url'); echo '<li class="member"><a href="'. $company_link .'" target="_blank">'.get_the_title().'</a></li>'; endwhile; echo '</ul></div>'; } else { echo '<p><strong>Er zijn geen bedrijven die voldoen aan deze zoekopdracht. Probeer het opnieuw.</strong></p>'; } } } ?>
I think the else was in the wrong place.
Anonymous(Private) March 28, 2017 at 2:22 pm #99928Hi Trevor,
I tried this at first and this is working but it gives me a message for every term. And I just want one message when there are no results (when all terms are empty).
I have use your code so you can check the page for yourself.
Best regards,
Joeri.Trevor(Private) March 28, 2017 at 2:50 pm #99933Ah, I see why. You are looping through custom terms one after the other, and only doing the actual wp_query inside each loop. That way you can have only one no results for each custom terms (the alphabet letters) or none at all. So, instead you have a test variable and set this to 0. Every time you find results in each loop, you increment that variable. If it is still 0 at the end, then no results were found:
<?php $custom_terms = get_terms('alfabet'); $empty_test = 0; if (!empty($custom_terms)) { foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array( 'post_type' => 'bontvrijlijst', 'search_filter_id' => 7242, 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'alfabet', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { $empty_test++; echo '<div id="' . $custom_term->slug . '" class="alfabet-letter-wrap"><div class="alfabet-letter">'.$custom_term->name.'</div><ul class="members">'; while($loop->have_posts()) : $loop->the_post(); $company_link = get_field('url'); echo '<li class="member"><a href="'. $company_link .'" target="_blank">'.get_the_title().'</a></li>'; endwhile; echo '</ul></div>'; } } } if ($empty_test > 0) { echo '<p><strong>Er zijn geen bedrijven die voldoen aan deze zoekopdracht. Probeer het opnieuw.</strong></p>'; } ?>
-
AuthorPosts