Support

Account

Home Forums Front-end Issues Filter Conditional Logic Sub Fields Based on Parent

Unread

Filter Conditional Logic Sub Fields Based on Parent

  • I am trying to create a search that filters posts by country and state/region/province. I have created some conditional logic sub fields based on country and now I am trying to figure out how to populate a select filter on my frontend similar to my backend.

    something like this (although I’d love for it to be as dynamic as possible, and not limited to me actually coding in the sub fields as elseif):

     $country = get_field('country');  //parent field
      $countrykey = 'country';
    $fieldcountry = get_field_object($countrykey ); 
     if ($fieldcountry) {
         echo '<select id="country"><option id="select">Select Country</option>';
           foreach ($fieldcountry['choices'] as $countrykey) {
               if (!empty($countrykey)) {
            echo '<option id="'. $countrykey .'>'. $countrykey .'</option>';
            } 
           }
          echo '</select>';
       }
      
     endif;
    
      if($country == 'USA') {
        $key = 'state_us'; //conditional logic based on country, would rather enter this dynamically
        $field = get_field_object($key); 
        if ($field) {
             echo '<select id="'. $key . '"><option id="select">Select Region</option>';
               foreach ($field['choices'] as $key) {
                   if (!empty($key)) {
                        echo '<option id="'.$key.'>'.$key.'</option>';
                    } 
               }
        }
    }
     elseif ($country == 'CA') {
         $key = 'province_ca'; 
        $field = get_field_object($key); 
        if ($field) {
             echo '<select id="'. $key . '"><option id="select">Select Region</option>';
               foreach ($field['choices'] as $key) {
                   if (!empty($key)) {
                        echo '<option id="'.$key.'>'.$key.'</option>';
                    } 
               }
        }
     } 
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.