Support Forums

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 Empty result message

Viewing 10 posts - 1 through 10 (of 11 total)
  • Joeri Marquerinck
    #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 Moderator
    #98590

    If 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.

    Joeri Marquerinck
    #99914

    Hi 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 Moderator
    #99926

    See 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.

    Joeri Marquerinck
    #99928

    Hi 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 Moderator
    #99933

    Ah, 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>';
    }
    ?>
    Joeri Marquerinck
    #99939

    Hi,

    It’s strange. When using if ($empty_test > 0) it’s not working. I don’t get the message.

    When using if ($empty_test >= 0) it is working but you also get the message when only a few terms have posts.

    Best regards,
    Joeri.

    Trevor Moderator
    #99943

    You might want to echo the $empty_test variable at each iteration to see what it is at?

    Joeri Marquerinck
    #99970

    Hi Trevor,

    It’s working! This one did the trick: if ($empty_test == 0) {

    Thank you for the great support!

    Best regards,
    Joeri.

    Trevor Moderator
    #100041

    Face -> Palm

    That was my bad in the coding. One = sets the parameter, Two tests it. D’oh. Are we done here?

Viewing 10 posts - 1 through 10 (of 11 total)

The topic ‘Empty result message’ is closed to new replies.