display a message if get form is empty

admin2025-06-02  1

I have a CPT, where i made a filter by taxonomies. Its a simple form, where users can filter by 2 taxonomies. If the filter returns no posts because its empty i want to show a message (ex. "Sorry no Events found")

And if its possible, i want to show (after form is submitted) the filter options, which were chosen by the user. I know how i could show this with method=post, but i dont have any idea for method=get.

Thanks for help, thats what i coded so far.

    <div class="leweb_events_search">
     <form method="get" action=""> 
        <div class="leweb_select">
        <?php
            if( $terms = get_terms( array( 'taxonomy' => 'events_category', 'orderby' => 'name' ) ) ) : 

                echo '<select name="events_category"><option value="">Select category...</option>';
                foreach ( $terms as $term ) :
                    echo '<option value="' . $term->name . '">' . $term->name . '</option>';
                endforeach;
                echo '</select>';
            endif;
        ?>
        </div>

        <div class="leweb_select">
        <?php
            if( $terms = get_terms( array( 'taxonomy' => 'events_car', 'orderby' => 'name' ) ) ) : 

                echo '<select name="events_car"><option value="">Select category...</option>';
                foreach ( $terms as $term ) :
                    echo '<option value="' . $term->name . '">' . $term->name . '</option>';
                endforeach;
                echo '</select>';
            endif;
        ?>  
        </div>

 <input type="button" value="submit">       

     </form>

    </div>

I have a CPT, where i made a filter by taxonomies. Its a simple form, where users can filter by 2 taxonomies. If the filter returns no posts because its empty i want to show a message (ex. "Sorry no Events found")

And if its possible, i want to show (after form is submitted) the filter options, which were chosen by the user. I know how i could show this with method=post, but i dont have any idea for method=get.

Thanks for help, thats what i coded so far.

    <div class="leweb_events_search">
     <form method="get" action=""> 
        <div class="leweb_select">
        <?php
            if( $terms = get_terms( array( 'taxonomy' => 'events_category', 'orderby' => 'name' ) ) ) : 

                echo '<select name="events_category"><option value="">Select category...</option>';
                foreach ( $terms as $term ) :
                    echo '<option value="' . $term->name . '">' . $term->name . '</option>';
                endforeach;
                echo '</select>';
            endif;
        ?>
        </div>

        <div class="leweb_select">
        <?php
            if( $terms = get_terms( array( 'taxonomy' => 'events_car', 'orderby' => 'name' ) ) ) : 

                echo '<select name="events_car"><option value="">Select category...</option>';
                foreach ( $terms as $term ) :
                    echo '<option value="' . $term->name . '">' . $term->name . '</option>';
                endforeach;
                echo '</select>';
            endif;
        ?>  
        </div>

 <input type="button" value="submit">       

     </form>

    </div>
Share Improve this question asked Mar 13, 2019 at 8:32 LovinQuaQuaLovinQuaQua 833 silver badges19 bronze badges 3
  • And what code do you use to get events? – Krzysiek Dróżdż Commented Mar 13, 2019 at 8:41
  • "I know how i could show this with method=post, but i dont have any idea for method=get" I don't see the difference in this context. What are you having trouble with? You just need to use $_GET instead of $_POST . – Jacob Peattie Commented Mar 13, 2019 at 8:45
  • This is so embarrassing, sorry for that post... – LovinQuaQua Commented Mar 13, 2019 at 9:12
Add a comment  | 

1 Answer 1

Reset to default 0

If anyone faces the same problem, its easy to display a message with have_posts()

<?php if ( have_posts() ) {
    echo "Events gefunden";
} else {
    echo "Keine Events gefunden";
}
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748811736a313923.html

最新回复(0)