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


acf/load_field doesn't seem to work

  • Hi,

    ACF Version : Advanced Custom Fields PRO

    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) {
      string(19) "field_56a20f6e381fc"
      string(25) "Catégories "de niveau 1""
      string(24) "produit_categories_niv_1"
      string(0) ""
      string(11) "post_object"
      string(0) ""
      string(0) ""
      string(0) ""
      array(3) {
        string(0) ""
        string(0) ""
        string(0) ""
      string(24) "produit_categories_niv_1"
      string(0) ""
      array(1) {
        string(18) "produits_categorie"
      array(0) {
      string(2) "id"
      array(4) {
        string(12) "Catégorie A"
        string(12) "Catégorie B"
        string(12) "Catégorie C"
        string(12) "Catégorie D"


  • 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” ?


  • 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.

    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)

The topic ‘acf/load_field doesn't seem to work’ is closed to new replies.