0

I'm trying to implement a custom search with php and wordpress. The search will be able to crawl through the post objects. According to the radio inputs the search should filter post after the custom taxonomy "kursart". Then we have the search input which should search after the keyword. I also need a dropdown which includes the "kursmethode" which is saved inside a meta field with the key "kursmethode".

EDIT: I'm using the shortcode [TEST]within my page to display my cusom search form as shown below. The Searchform will link to the following url http://mydomain.de/startiq/?s=. That site is created by wordpress's own search.phpwhich of course i modified to look the way i want it to. The search here is generated by the plugin Ultimate WP Query Search Filter and the following shortcode [ULWPQSF id=9070 text_position="top" formtitle="0" button=0]. This form is using ajax to display the search results.

Screenshot

The searchquery this first form is executing looks like this:

http://mydomain.de/?s=

My own Custom form that i want to use on my homepage will link to this page My form will look like that:

Screenshot Search Form

And this is my code till now:

function startkurs(){

          // Erzeuge Form für die Ausgabe im Frontend
    ?>

    <div class="container">
        <form class="form cf search-input-form" method="get" action="<?php echo get_site_url(); ?>"  id="form-all">
            <section class="plan cf">
                <input type="radio" name="kursart" id="alle-kurse" value="alle-kurse"><label class="free-label four col" for="alle-kurse">Alle Kurse</label>
                <input type="radio" name="kursart" id="studiengaenge" value="studiengaenge" checked><label class="basic-label four col" for="studiengaenge">Studiengänge</label>
                <input type="radio" name="kursart" id="ausbildung" value="ausbildung"><label class="premium-label four col" for="ausbildung">Ausbildung</label>
                <input type="radio" name="kursart" id="seminare" value="seminare"><label class="test-label four col" for="seminare">Semiare</label>
            </section>

            <div class="lil-spacer"></div>

                              <div class="form_inputs" style="width: 83%;">

                    <input type="text" name="s" size="30" value="<?php the_search_query(); ?>" placeholder="Finden Sie Ihren Kurs ..." class="text-search" />

                    <select class="btn-methodik" name="methode">
                        <option value="all" selected > -- Auswählen -- </option>
                        <option value="Präsenzveranstaltung">Präsenzveranstaltung</option>
                        <option value="Online">Online</option>
                        <option value="Fernunterricht">Fernunterricht</option>
                        <option value="Blended">Blended</option>
                        <option value="Inhouse">Inhouse</option>
                    </select>

                              </div>

                              <button type="submit" name="fulltext" class="btn-search" form="form-all" value="Suchen">Suchen</button >


        </form>

        <!-- button type="submit" name="fulltext" class="btn-search" form="form-all" value="Suchen">Suchen</button -->

        <?php
        $search_field = $_GET['s']; // get the search input
        $my_select_taxonomy = $_GET['methode']; // get the selected value


                    $radio = isset($_POST['kursart']);
                    switch ($radio){
                              case 'alle-kurse':
                                        $args = array(
                        's' => $search_field,
                        'post_type' => 'post', // Search in Posts
                        'post_per_page' => 10, // Display 10 Posts
                        'orderby'=> 'title', // Order Results after their title
                        'order' => 'DESC', // order in descending order
                        'meta_key' => 'kursmethode', // search inside the selected meta field
                        'meta_value' => $my_select_taxonomy, // search inside the meta field according to the selected value
                        'meta_compare' => 'LIKE', // only show results that equal the selected term
                    );
                                        break;
                              case 'studiengaenge':
                                        $args = array(
                        's' => $search_field,
                        'post_type' => 'post', // Search in Posts
                        'post_per_page' => 10, // Display 10 Posts
                        'orderby'=> 'title', // Order Results after their title
                        'order' => 'DESC', // order in descending order
                        'meta_key' => 'kursmethode', // search inside the selected meta field
                        'meta_value' => $my_select_taxonomy, // search inside the meta field according to the selected value
                        'meta_compare' => 'LIKE', // only show results that equal the selected term
                        // 'tax_query' => array(
                        //  array(
                        //      'taxonomy' => 'kursart',
                        //      'field'    => 'term_id',
                        //      'terms'    => array( 609,620,621,622 ),
                        //      'operator' => 'IN'
                        //  ),
                        // ),

                    );
                                        break;
                              case 'ausbildung':
                                        $args = array(
                        's' => $search_field,
                        'post_type' => 'post', // Search in Posts
                        'post_per_page' => 10, // Display 10 Posts
                        'orderby'=> 'title', // Order Results after their title
                        'order' => 'DESC', // order in descending order
                        'category__in' => '608',
                        'meta_key' => 'kursmethode', // search inside the selected meta field
                        'meta_value' => $my_select_taxonomy, // search inside the meta field according to the selected value
                        'meta_compare' => 'LIKE', // only show results that equal the selected term
                        // 'tax_query' => array(
                        //  array(
                        //      'taxonomy' => 'kursart',
                        //      'field'    => 'term_id',
                        //      'terms'    => array( 608 ),
                        //      'operator' => 'IN'
                        //  ),
                        // ),
                    );
                                        break;
                              case 'seminare':
                                        $args = array(
                        's' => $search_field,
                        'post_type' => 'post', // Search in Posts
                        'post_per_page' => 10, // Display 10 Posts
                        'orderby'=> 'title', // Order Results after their title
                        'order' => 'DESC', // order in descending order
                        'meta_key' => 'kursmethode', // search inside the selected meta field
                        'meta_value' => $my_select_taxonomy, // search inside the meta field according to the selected value
                        'meta_compare' => 'LIKE', // only show results that equal the selected term
                        // 'tax_query' => array(
                        //  array(
                        //      'taxonomy' => 'kursart',
                        //      'field'    => 'term_id',
                        //      'terms'    => array( 606,614,610,611,615,612,613 ),
                        //      'operator' => 'IN'
                        //  ),
                        // ),
                    );
                                        break;

                    }





        $query = new WP_Query( $args );

        ?>

    </div>

    <?php
}

add_shortcode('TEST', 'startkurs');

It produces a link like that:

http://mydomain.de/?kursart=alle-kurse&s=content&methode=all&fulltext=Suchen

But i need the link to look like that to get the search results on the according page:

http://mydomain.de/?s=content&kursart=alle-kurse&methode=all

I dont understand what i'm doing wrong here.

EDIT 2: If i'm deleting the part with the radio buttons everything works as expected.

function startkurs(){

          // Erzeuge Form für die Ausgabe im Frontend
    ?>

    <div class="container">
        <form class="form cf search-input-form" method="get" action="<?php echo get_site_url(); ?>"  id="form-all">

                              <section class="plan cf">
                <input type="radio" name="kursart" id="alle-kurse" value="alle-kurse"><label class="free-label four col" for="alle-kurse">Alle Kurse</label>
                <input type="radio" name="kursart" id="studiengaenge" value="studiengaenge" checked><label class="basic-label four col" for="studiengaenge">Studiengänge</label>
                <input type="radio" name="kursart" id="ausbildung" value="ausbildung"><label class="premium-label four col" for="ausbildung">Ausbildung</label>
                <input type="radio" name="kursart" id="seminare" value="seminare"><label class="test-label four col" for="seminare">Semiare</label>
            </section>

            <div class="lil-spacer"></div>



                              <div class="form_inputs" style="width: 83%;">

                    <input type="text" name="s" size="30" value="<?php the_search_query(); ?>" placeholder="Finden Sie Ihren Kurs ..." class="text-search" />

                    <select class="btn-methodik" name="methode">
                        <option value="all" selected > -- Auswählen -- </option>
                        <option value="Präsenzveranstaltung">Präsenzveranstaltung</option>
                        <option value="Online">Online</option>
                        <option value="Fernunterricht">Fernunterricht</option>
                        <option value="Blended">Blended</option>
                        <option value="Inhouse">Inhouse</option>
                    </select>



                              </div>



                              <button type="submit" name="fulltext" class="btn-search" form="form-all" value="Suchen">Suchen</button >


        </form>

        <!-- button type="submit" name="fulltext" class="btn-search" form="form-all" value="Suchen">Suchen</button -->

        <?php
        $search_field = $_GET['s']; // get the search input
        $my_select_taxonomy = $_GET['methode']; // get the selected value

                    $args = array(
                              's' => $search_field,
                              'post_type' => 'post', // Search in Posts
                              'post_per_page' => 10, // Display 10 Posts
                              'orderby'=> 'title', // Order Results after their title
                              'order' => 'DESC', // order in descending order

           );

        $query = new WP_Query( $args );

        ?>

    </div>

    <?php
}

add_shortcode('TEST', 'startkurs');

But i need the buttons in the position i showed above to query the search accordingly.

I appriciate your help.

  • 1
    The order of the parameters doesn't matter, what exactly is wrong here? And your example of what you want has s twice. – Jacob Peattie Apr 13 '18 at 10:39
  • @JacobPeattie oh damn i have made an error while copy and pasting the code sorry... when i search for my query i will be redirected to a special page that is called by the .../?s=parameter after the slash. it only functions that way. so i'm really clueless – Bexx Apr 13 '18 at 10:43
  • s is the parameter reserved by WordPress for the standard search template. It will be pulled into the main query and interpreted as a search through the standard search form/widget. If you're not using that and are using a custom query you need to choose something else, like search, or q. – Jacob Peattie Apr 13 '18 at 10:47
  • I'm using the custom search wich is modiefied – Bexx Apr 13 '18 at 10:57
  • You're not though, you're using a secondary query in a shortcode. – Jacob Peattie Apr 13 '18 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.