Support

Account

Home Forums Backend Issues (wp-admin) acf/load_field doesn't seem to work

Solved

acf/load_field doesn't seem to work

  • Hi,

    ACF Version : Advanced Custom Fields PRO 5.3.2.2

    I have an option field, named produits_categories_niv_1 (mind the “s” at the end of the first word), which contains a selection several “categories” (a custom post type),.
    I have a field, visible in the custom post type “produit”n which is called produit_categories_niv_1 (no “s” at the end of the first word).
    I want the second one to display only the options that have been selected in the first one.

    1. My hook seems to have no effect on the content of the displayed select
    2. But when i make a var_dump of the field at the end of the hook, there is a “choices” index, that contains the array, each item begin post_id => post_title.

    What am i doing wrong, please ?

    My hook is as follows :

    function acf_load_produit_categories_niv_1_choices($field) {
        // reset choices
        $field["choices"] = [];
        
        $choices = get_field("produits_categories_niv_1", "option");
    
        // loop through array and add to field 'choices'
        if(is_array($choices)) {
            foreach($choices as $categorie) {
            	$value = $categorie->ID;
            	$label = $categorie->post_title;
                $field["choices"][$value] = $label;
            }
        }
        
        // return the field
        return $field;
    }
    add_filter('acf/load_field/name=produit_categories_niv_1', 'acf_load_produit_categories_niv_1_choices');
    

    And here is the var_dump of the field just before return

    array(25) {
      ["ID"]=>
      int(224)
      ["key"]=>
      string(19) "field_56a20f6e381fc"
      ["label"]=>
      string(25) "Catégories "de niveau 1""
      ["name"]=>
      string(24) "produit_categories_niv_1"
      ["prefix"]=>
      string(0) ""
      ["type"]=>
      string(11) "post_object"
      ["value"]=>
      NULL
      ["menu_order"]=>
      int(3)
      ["instructions"]=>
      string(0) ""
      ["required"]=>
      int(1)
      ["id"]=>
      string(0) ""
      ["class"]=>
      string(0) ""
      ["conditional_logic"]=>
      int(0)
      ["parent"]=>
      int(9)
      ["wrapper"]=>
      array(3) {
        ["width"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["id"]=>
        string(0) ""
      }
      ["_name"]=>
      string(24) "produit_categories_niv_1"
      ["_input"]=>
      string(0) ""
      ["_valid"]=>
      int(1)
      ["post_type"]=>
      array(1) {
        [0]=>
        string(18) "produits_categorie"
      }
      ["taxonomy"]=>
      array(0) {
      }
      ["allow_null"]=>
      int(0)
      ["multiple"]=>
      int(1)
      ["return_format"]=>
      string(2) "id"
      ["ui"]=>
      int(1)
      ["choices"]=>
      array(4) {
        [82]=>
        string(12) "Catégorie A"
        [213]=>
        string(12) "Catégorie B"
        [214]=>
        string(12) "Catégorie C"
        [215]=>
        string(12) "Catégorie D"
      }
    }

    Thanks

  • The problem is that the field that you’re trying to change the choices on is a post_object field. You can’t dynamically set the choices of this type of field this way. You would need to change it to a select field.

  • Ok, so, if i understand correctly, you’re telling me that the field i’m using (the second one, which should refer to the values in the optin field) is not managed as a select, but as a post itself, and the result of that is shown as a select (stop me anywhere i’m telling bulshit).
    So, either there is a way to use a post_object, which would need a way to get only the posts selected in the option field, or i should use a select, populate it with ids and titles from those posts, and manage it myself afterwards.

    1. Am i correct ?
    2. Is there a way to use a post_object, and let only the ones selected in the option field “bubble up” ?

    Thanks

  • Correct.

    The post object field does have filters that can be used to alter the query used to populate the selections. But in may or may not work in your case. I have not tried this, and the odds may not be good for getting it working. I’ll explain why below.

    http://www.advancedcustomfields.com/resources/acf-fields-post_object-query/

    you could add a filter that gets the values from the option and then adds a post__in argument to the query.

    Why this may not work. There is a function in ACF that runs after the query that groups the posts by post_type, taxonomy and parent posts. When this function runs if there are hierarchical post types in the mix and the parent post of the post that you want to show is not also in the group then there’s a good chance that the post you want will not appear.

  • Ok, i’m gonna test this, and i’ll post the results here.

    Thanks a lot for this answer, which was as complete as it was precise (yes, non-native speakers can be verbose 🙂 ).

  • Works like a charm : field 1 get only categories defined in the option field, plus one other option field, field 2 get all categories except those ones.
    Thanks a lot, John !

    // Uniquement les catégories "de niveau 1" (y compris la mise en avant "nouveautés")
    function suremesurev1_produit_categories_niv_1_query($args, $field, $post) {
    	$choices = get_field("produits_categories_niv_1", "option");
    	$choices[] = get_field("produits_categorie_mise_en_avant", "option");
    	$args["post__in"] = $choices;
    
        return $args;
    }
    add_filter('acf/fields/post_object/query/name=produit_categories_niv_1', 'suremesurev1_produit_categories_niv_1_query', 10, 3);
    
    // Toutes les catégories, sauf celles "de niveau 1", ni la mise en avant "nouveautés"
    function suremesurev1_produit_categories_query($args, $field, $post) {
    	$choices = get_field("produits_categories_niv_1", "option");
    	$choices[] = get_field("produits_categorie_mise_en_avant", "option");
    	$args["post__not_in"] = $choices;
    
        return $args;
    }
    add_filter('acf/fields/post_object/query/name=produit_categories', 'suremesurev1_produit_categories_query', 10, 3);
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.