Support

Account

Home Forums Backend Issues (wp-admin) Select field. Unwanted filtering.

Helping

Select field. Unwanted filtering.

  • Hi.
    I’m using a select field, whose options are loaded dynamically(Use AJAX to lazy load choices?). The problem that I’m facing is that the options get filtered in order to contain the value of the select field, if an option does not contain the value of the select field it gets discarted.. E. g.:

    
    <strong>Select field</strong> value: 'hello world'
    Ajax result : ['hello world, today is monday', 'hello world, it\'s monday', 'today is monday']
    Displayed options in the <strong>select field</strong>: ['hello world, today is monday', 'hello world, it\'s monday']
    

    As we can see from the example the 'today is monday' option gets distracted.
    Is there any way to prevent this?

    Here is the interesting part of the code: function.php

    
    function my_custom_func( $field ) {
      $select_field_value = $_POST['s'];
      /*Using curl to retrieve a json with the options. Stored in $json.*/
      $response = json_decode($json, true);
    
      $choices = array();
      if ($response['results']){
        foreach ($response['results'] as $entity) {
          /*The actual code does more things, obviated in order to keep it simple.*/
          $id = $entity['id'];
          choices[$id] = entity['value'];
        }
        $field['choices'] = $choices;
        return $field;
      }
    }
    
    add_filter('acf/load_field/name=my_custom_field', 'my_custom_func');
    

    thx.

  • Hi @hivanov

    I’m afraid I don’t understand the issue here. Could you please explain it in more detail? Maybe you can share some screenshots of the issue and your setup?

    Thanks!

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.