I’m a bit stuck with custom search I inherited. My goal is for list not to load when search term is empty, I tried adding a if condition with
(empty($search)){return;} in several ways but nothing seems to work.

/** edited out bunch of filter caterogies

                <div class="col-md-3 col-sm-3">
                    <input type="submit" name="submit" value="Search" class="widget-button">
                </div>



<div class="container">
    <div class="row">

        <div class="col-md-12">

        <ul class="qualification-listing">

    <?php

        $search = w_qualification_search();
        if ($search->have_posts()) :
        while ( $search->have_posts() ) {

            $search->the_post();

            $level = wp_get_post_terms( get_the_ID(), 'qualification_level');
            if (isset($level[0])) $level = $level[0]->name; // what if not?

            $code = get_post_meta( get_the_ID(), 'wa_code', true );
            $download = get_post_meta( get_the_ID(), 'qualification_upload', true );

            $terms = get_the_terms( get_the_ID(), 'qualification_sector' );

            $slug = '';
            if ( is_array( $terms ) ){
                $term = array_shift( $terms );
                $slug = $term->slug;
            }
            elseif ( ! empty($terms)) { 
                $term = $terms;
                $slug = $term->slug;
            }

              ?>              
            <li class="<?php if (isset($term)) the_field('colourway', $term ); ?>">
                <div class="qualification-listing-level">Level <span class="<?php echo $level; ?>"><?php echo $level; ?></span></div>
                <h4><?php echo get_the_title(); ?></h4>
                <span class="qualification-code">W Code: <?php echo $code; ?></span>
                <div>
                    <a class="widget-button" href="<?php echo get_permalink(73) . '?centre_qualification=' . get_the_ID(); ?>"><span class="glyphicon glyphicon-map-marker"></span> View Centres offering this Qualification</a>
                    <a class="widget-button pull-right" href="<?php echo $download['url']; ?>" download><span class="glyphicon glyphicon-download-alt"></span> Download Details</a>
                </div>

            </li>



       <?php
        }

        else: ?>

          <h3 class="no-results title">Nothing found. Perhaps broaden your 
       search criteria.</h3>



<?php
        endif;
        wp_reset_postdata(); ?>

    </ul>

    </div>


   </div>
</div>


</main>
<?php get_template_part('template-parts/prefooter'); ?>
<?php get_footer();

Read more here: PHP, return when search terms are empty


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: