Support

Account

Home Forums General Issues How to make a Front End search Form with ACF

Unread

How to make a Front End search Form with ACF

  • I have looked for more than 2 Weeks how to make a Search and Filter Function using ACF Fields and I have finally Made it using this Code which I would love to share it with you.

    First university-search.php

    <?php
     $is_search = count( $_GET );
        $university_studyfield = get_terms([
                'taxonomy' => 'university_studyfield',
                'hide_empty' => false,
        ]);
        $study_degree = get_terms([
                'taxonomy' => 'study_degree',
                'hide_empty' => false,
        ]);
     $university_location = get_terms([
                'taxonomy' => 'university_location',
                'hide_empty' => false,
        ]);
      
    ?>
    <div class="container">
           <form action="<?php echo home_url('/');?>">
      <div class="row form-group  " style="justify-content: center;">
       
     
             <div class="col-lg-4"> 
    
                   
                    <select name="university_studyfield" class="form-control">
    
                            <option value=""><?php _e('Study Field','studyshoot'); ?></option>
    
                            <?php foreach($university_studyfield as $university_studyfield):?>
                                    <option 
                                    
                                    <?php if(  isset($_GET['university_studyfield']) && ( $_GET['university_studyfield'] == $university_studyfield->slug)  ):?>
                                        selected
                                    <?php endif;?>
    
                                    value="<?php echo $university_studyfield->slug;?>"><?php echo $university_studyfield->name;?></option>
                            <?php endforeach;?>
    
                    </select>
                
                </div>
     <div class="col-lg-4"> 
                    
                
                    <select name="study_degree" class="form-control">
    
                            <option value=""><?php _e('  Degree  ','studyshoot'); ?> </option>
    
                            <?php foreach($study_degree as $study_degree):?>
                                    <option 
                                    
                                    <?php if(  isset($_GET['study_degree']) && ( $_GET['study_degree'] == $type->slug)  ):?>
                                        selected
                                    <?php endif;?>
    
                                    value="<?php echo $study_degree->slug;?>"><?php echo $study_degree->name;?></option>
                            <?php endforeach;?>
    
                    </select>
                
      </div>   
     
             <div class="col-lg-4"> 
    
                  
                
                    <select name="university_location" class="form-control">
    
                            <option value=""><?php _e('  Location   ','immobilien'); ?></option>
    
                            <?php foreach($university_location as $university_location):?>
                                    <option 
                                    
                                    <?php if(  isset($_GET['university_location']) && ( $_GET['university_location'] == $university_location->slug)  ):?>
                                        selected
                                    <?php endif;?>
    
                                    value="<?php echo $university_location->slug;?>"><?php echo $university_location->name;?></option>
                            <?php endforeach;?>
    
                    </select>
                
                </div>
                </div>
            <button type="submit" class="new-btn-one btn-block "><?php _e('Search Now ','studyshoot'); ?> </button>
         
            
    
                </form>
    </div>
     
    <br><br>
     

    Then Functions.php

    
    function search_query()
    {
    
        $paged = ( get_query_var('paged')  )  ? get_query_var('paged') : 1; 
    
        $args = [
    
            'paged' => $paged,
            'post_type' =>  array('grants','_'),
            'tax_query' => [],
     
    
        ];
    
     
    
         if( isset($_GET['university_studyfield']) )
        {
            if(!empty($_GET['university_studyfield']))
            {
                $args['tax_query'][] = [
    
                        'taxonomy' =>  'university_studyfield',
                        'field' => 'slug',
                        'terms' => array( sanitize_text_field( $_GET['university_studyfield'] ) )
    
                ];
            }
        }
    
     
    
         if( isset($_GET['study_degree']) )
        {
            if(!empty($_GET['study_degree']))
            {
                $args['tax_query'][] = [
    
                        'taxonomy' =>  'study_degree',
                        'field' => 'slug',
                        'terms' => array( sanitize_text_field( $_GET['study_degree'] ) )
    
                ];
            }
        }
    
             if( isset($_GET['university_location']) )
        {
            if(!empty($_GET['university_location']))
            {
                $args['tax_query'][] = [
    
                        'taxonomy' =>  'university_location',
                        'field' => 'slug',
                        'terms' => array( sanitize_text_field( $_GET['university_location'] ) )
    
                ];
            }
        }
     
     
    
        return  new WP_Query($args);
    
    }
    

    Then I generate the Shortcode to use it in My Template:

    I still need a Tutorial where I can learn how to make it with ajax a live search. also as you see I used the Custom Taxonomy field to do that…. How do I replace the Taxonomy with ACF Select field if is it Possoble

    Thanks for all who are working at ACF, It is a Great one

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.