Support

Account

Home Forums Pre-purchase Questions Select field question

Solving

Select field question

  • Hello,

    i am wondering if there is a way to make a select field with 10 values BUT display the “not already selected” ones?

    So in a way use this select box to order posts on the page?

    Thanks,
    Alan

  • Hi @herka,

    Hmm… This should be possible using the acf/load_field filter and array_diff(...) PHP method. Attach acf/load_field filter to your select then the code should be something like this:

    
    add_filter('acf/load_field/type=select', 'my_function');
    
    function my_function($field) {
    	$value = get_field($field_id, $post_id);
    	$choices = $field['choices'];
    	$field['choices'] = array_diff($choices, $value);
    	return $field;
    }
    

    Hope this helps 🙂 Have a look at: http://php.net/manual/en/function.array-diff.php, http://www.advancedcustomfields.com/resources/acfload_field/ for more information

  • Thanks for your replay!

    Im having a problem cause the function halts on get_field line…

    My select field is called “position” and field group is “doctors” and its called on custom post type so the user can select “order” of the items on the page.

    $value = get_field(“position”,$post_id);

    i tried with variations from http://www.advancedcustomfields.com/resources/get_field/ but it keep hanging.

    Any help would be appreciated.

    Alan

  • Hi @herka

    You have to replace the $post_id with the ID of the post which contains the ACF select field

  • function my_function_select($field) {
    $post_id = 10;
    $field_id= ‘position’;
    $value = get_field($field_id, $post_id);
    $choices = $field[‘position’];
    $field[‘position’] = array_diff($choices, $value);
    return $field;
    }
    add_filter(‘acf/load_field/name=position’, ‘my_function_select’);

    $post_id=10; = custom post type where i have select dropdown (10 is actual id of one of the posts where i have the select…)
    $field_id=’position’; = name of the select field

    Can this be an issue because im using custom post types?

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

You must be logged in to reply to this topic.