Forums Forums Search & Filter Pro Never get "no results" message while using shortcode loop

Viewing 3 posts - 1 through 3 (of 3 total)
  • Anonymous
    #200055

    Hi, we are using S&F with Elementor, and to get results for a custom post type & custom fields to display how we wanted them to, we made a shortcode with a loop. It works great except for one thing: it never shows a “no results” message. If I search for “sdfnkjsnsdnf” or other nonsense, it displays all the posts instead of the no results message. Otherwise, it filters properly. Here is our shortcode:

    function tm_show_testimonies( $atts ) {
    
        // The Query
        $args = array('post_type' => 'testimony_cpt');
        $args['search_filter_id'] = 13551;
        $the_query = new WP_Query( $args );
    
        // The Loop
        if ( $the_query->have_posts() ) {
            // Wrapper div
            $output .= '<div class="tm-testimonies-list">';
            // Column headings
            $output .= '<div class="tm-testimony column-headings">';
                $output .= '<div>Name & Location</div>';
                $output .= '<div>Date</div>';
                $output .= '<div>Category</div>';
                $output .= '<div>Testimony</div>';
            $output .= '</div>';
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                $tmyID= get_the_ID();
                $tmyTerms = get_the_terms( $tmyID, 'testimony_tags' );
                $termResults = [];
                    // Make the terms array into a string
                    if ($tmyTerms) {
                        foreach( $tmyTerms as $term ) {
                            $termResults[] = $term->name;
                            $tmyTags = implode(", ",$termResults);
                        }
                    } else {
                        $tmyTags = "";
                    }
                $tmyDate = get_the_date( 'F j, Y' );
                
                // Get custom fields
                $tmyName = get_post_meta( $tmyID, 'testimony_firstname', true ) . ' ' . get_post_meta( $tmyID, 'testimony_lastname', true );
                $tmyLocation = get_post_meta( $tmyID, 'testimony_location', true );
                
                // Format the output
                $output .= '<div class="tm-testimony">';
                    $output .= '<div><span class="testimony-name">' . $tmyName . '</span><br><span class="testimony-location">' . $tmyLocation . '</span></div>';
                    $output .= '<div>' . $tmyDate . '</div>';
                    $output .= '<div>' . $tmyTags . '</div>';
                    $output .= '<div><h3 class="testimony-title">' . get_the_title() . '</h3>';
                    $output .= '<p>' . get_the_excerpt() . '</p></div>';
                $output .= '</div>';
            }
            $output .= '</div>';
            /* Restore original Post Data */
            wp_reset_postdata();
        } else {
            //No Results
            $output = '<div class="search-filter-results-list" data-search-filter-action="infinite-scroll-end"><span>No Results Found</span></div>';
        }
    
        // Return shortcode output
        return $output;
    }

    Can you tell me if there is something we need to change in order for S&F to show a no results message? Thanks!

    Trevor
    #200124

    Hi

    This is an alternative exemplar results.php file content that might do as you want:

    <?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
     *
     */
    
    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='search-filter-result-item'>
        <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>
        
        <hr />
       </div>
       
       <?php
      }
     ?>
     </div>
    <?php
    } else {
     
     //figure out which type of "no results" message to show
     $message = "noresults"; 
     if(isset($query->query['paged'])) {  
      if($query->query['paged']>1){
       $message = "endofresults";
      }
     }
     
        if($message=="noresults") {
        ?>
     <div class='search-filter-results-list' data-search-filter-action='infinite-scroll-end'>
      <span>No Results Found</span>
     </div>
     <?php
        } else {
     ?>
     <div class='search-filter-results-list' data-search-filter-action='infinite-scroll-end'>
      <span>End of Results</span>
     </div>
     <?php
     }
    }
    ?>
    Anonymous
    #200327

    Excellent, thanks! Using the built-in S&F results shortcode and customizing the results template you provided was a much better way to handle this, and is working as expected.

Viewing 3 posts - 1 through 3 (of 3 total)