Support

Account

Forum Replies Created

  • Hey @hube2,
    I just found out that I need to use $field['value'] instead of $field['default_value'] in acf/prepare_field to do what I want to do! 🙂

    Cheers,
    Moritz

  • Hi @hube2,
    I have currently split my code for loading the choices for my checkboxes and for setting the default value. I hope my code gives you enough insight to understand what I’m trying to do.

    function wptd_acf_load_brot_verfuegbarkeit_values( $field ) {
    
        if (!is_admin()) {
            // Only in frontend
            $verfuegbarkeiten = get_query_var('verfuegbarkeiten') ?? $_SESSION['verfuegbarkeiten'] ;
            $sortiment_heute = get_query_var('sortiment_heute') ?? $_SESSION['sortiment_heute'];
    
            if (!empty($sortiment_heute)) {
                $field['default_value'] = wptd_get_brote_available($sortiment_heute, $verfuegbarkeiten);
            }
        }
    
        return $field;
    }
    add_filter('acf/load_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_values');
    
    function wptd_acf_load_brot_verfuegbarkeit_choices( $field ) {
    
        if (!is_admin()) {
            // Only in frontend
            $sortiment_heute = get_query_var('sortiment_heute');
    
            if (!empty($sortiment_heute)) {
                $field['choices'] = array_reduce($sortiment_heute, function($result, $item) {
                    $result[$item] = get_the_title($item);
                    return $result;
                }, []);
            }
        }
    
        return $field;
    }
    add_filter('acf/prepare_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_choices');

    Cheers,
    Moritz

  • Hi @hube2,
    currently, I’ve split up the code into setting the available choices for the checkboxes in acf/prepare_field and defining the default value for the field in acf/load_field. I hope my code below is enough to understand what I’m doing.

    
    function wptd_acf_load_brot_verfuegbarkeit_values( $field ) {
    
        if (!is_admin()) {
            // Only in frontend
            $verfuegbarkeiten = get_query_var('verfuegbarkeiten') ?? $_SESSION['verfuegbarkeiten'] ;
            $sortiment_heute = get_query_var('sortiment_heute') ?? $_SESSION['sortiment_heute'];
    
            if (!empty($sortiment_heute)) {
                $field['default_value'] = wptd_get_brote_available($sortiment_heute, $verfuegbarkeiten);
            }
        }
    
        return $field;
    }
    add_filter('acf/load_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_values');
    
    function wptd_acf_load_brot_verfuegbarkeit_choices( $field ) {
    
        if (!is_admin()) {
            // Only in frontend
            $sortiment_heute = get_query_var('sortiment_heute');
    
            if (!empty($sortiment_heute)) {
                $field['choices'] = array_reduce($sortiment_heute, function($result, $item) {
                    $result[$item] = get_the_title($item);
                    return $result;
                }, []);
            }
        }
    
        return $field;
    }
    add_filter('acf/prepare_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_choices');
    
  • Thanks for your reply, @elliot.
    We’re experiencing this issue with the classic editor though..
    Any chance to get this working at least for the classic editor?

    Cheers,
    Moritz

  • Hi @elliot,
    this is still an issue for us in the most current version (5.8.9).

    I was able to narrow it down to acf_get_valid_post_id() which seems to return a wrong post_id in these preview cases – don’t know whether the returned post_id is of a revision? I don’t quite get everything that’s going on, tbh.

    Could you please have a look? Would be great to get this working again. Our clients are asking for it, they would like to preview drafts…

    Cheers mate 😉
    Moritz

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