Support

Account

Home Forums Backend Issues (wp-admin) Dropdown value saved but not loaded back

Solved

Dropdown value saved but not loaded back

  • Hi folks,

    I’ve an issue directly linked to the following work (scroll to the end for the solution).
    Basically, I’m saving one or more dropdown fields value whose select options have been automatically set by some javascript.
    This is working perfectly, I can see the related custom fields set on the database, with the exact value I chose.

    But when refreshing my page (in admin), my dropdowns are not filled with the database value (see attached file). They are just empty, while every other fields are loading back with their own value.

    I’ve the feeling the issue could be related to the fact that my dropdown field have been created without values (since I’m setting them in javascript). When building the page (in admin), ACF could try to match the default values of the field to the one on the database and since it fails, displays an empty dropdown.

    Any idea?

  • Hi @tazintosh

    I think you are correct. In this case, you need to set the choices dynamically, based on the value in the database (and the gallery if you want to). To do that, you need to use the acf/load_field hook. To learn more about it, kindly check this tutorial: https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/.

    I hope this helps 🙂

  • Hi James,

    I’ll give a look, thank you!
    However, isn’t the actual behavior a little weird?
    I mean, if the “mismatch” is really the cause here, why AFC make the choice of “empty” rather than simply loading the database value as fallback?
    Perhaps I’m missing a technical point here, but for now, it makes much more sense to me.

  • Hi @tazintosh

    That’s because the select field tries to show the label instead of the value itself. To get the label, the select field will compare the value with the choices you’ve set on the field group editor page. But because it’s not there, then the select field will think that this is not the correct value (in case you changed the field type) and displays nothing.

    I hope this makes sense 🙂

  • Hi James,
    Your explanation makes sense (don’t worry) 😉
    However, the fallback choice doesn’t ^^
    To me, a database value, in the very last case, should prime over the rest.

  • Hi @tazintosh

    This behavior is used for all of the fields as default. Imagine you have a text field, and then you decided to change it to a post object field (which is saved as ID in the database). Because post object needs the ID to get the post but the database still has the text from the text field before, the post retrieval will be failed.

    Now imagine if the post object field shows the text instead of empty while the other post object fields show the post title, it sure will cause confusion because you won’t know which one has the correct value.

    But if you think the default behavior should be showing the value in the database, you can always open a new ticket and submit a feature request. You can open a new ticket here: https://support.advancedcustomfields.com/new-ticket.

    Hope this helps 🙂

  • Hi James,

    Sorry for the delay, I had to work on other projects.
    I’ve tried using acf/load_field, sadly without luck.
    I think I also need to use acf/load_value.

    Here’s what I’ve done so far:

    $imageSizeSelect = 'field_5831de3000470';
    global $databaseValue;
    
    function my_acf_load_value($value, $post_id, $field){
    	$databaseValue = $value;
    }
    add_filter('acf/load_value/key=' . $imageSizeSelect, 'my_acf_load_value', 9, 3);
    
    function my_acf_load_field($field){
    	$field['choices'][$databaseValue] = $databaseValue;
    	return $field;
    }
    add_filter('acf/load_field/key=' . $imageSizeSelect, 'my_acf_load_field', 10, 1);

    I’m clearly missing something (which by chance will seems obvious to you).
    First thing I’ve noticed, it that load_field is always run before load_value, which implies I’m getting an empty value.

  • Hi @tazintosh

    I’m not sure how you get the $databaseValue variable, but I believe you need to get the database value by using the get_field() function instead. For a single select field, you should be able to do it like this:

    function my_acf_load_database_value($field){
        // get the current post
        global $post;
        
        // get the database value
        $db_value = get_field($field['name'], $post->ID, false);
        
        //set the choices
    	$field['choices'][$db_value] = $db_value;
        
    	return $field;
    }
    add_filter('acf/load_field/key=' . $imageSizeSelect, 'my_acf_load_field', 20, 1);

    But is seems you are using the repeater field to contain the select fields. In this case, you can try the following code:

    function my_acf_load_database_value($field){
        // get the current post
        global $post;
        
        // get the database value
        $db_value = get_field('repeater_field_name', $post->ID, false);
        
        // loop through the repeater value
        foreach( $db_value as $row ){
            
            // get the current row value
            $row_select_value = $row[$field['key']];
            
            // set the choices
            $field['choices'][$row_select_value] = $row_select_value;
        }
        
        // return
    	return $field;
    }
    add_filter('acf/load_field/key=' . $imageSizeSelect, 'my_acf_load_field', 20, 1);

    If that doesn’t work, could you please share the JSON export file of your field group so I can test it out on my installation?

    Thanks 🙂

  • Hi James,

    Thanks again for you time.
    There is obviously informations missing on my side, so please allows me to provide some details:

    All of the following is for the admin backend only
    • I’m editing an article, on which is attached a flexible content field.
    • This flexible content, contains (among others) a gallery group of fields (I’m using the “clone” field type into my flexible content field for that).
    • This gallery group of fields contains (among others), a repeater field named “image_size”
    • This “image_size” repeater contains the select (image_reference) I want to hook and populate with the database value.

    Here’s a draw:

    ┌─────────────────────────────────────────────────────────────────────┐
    │Fields Group (Flexible Content)                                      │
    │                                                                     │
    │  ┌---------------------------------------------------------------┐  │
    │  │                      Some Fields Groups                       │  │
    │  └---------------------------------------------------------------┘  │
    │  ┌───────────────────────────────────────────────────────────────┐  │
    │  │Gallery Fields Group                                           │  │
    │  │                                                               │  │
    │  │ ┌-----------------------------------------------------------┐ │  │
    │  │ │                        Some Fields                        │ │  │
    │  │ └-----------------------------------------------------------┘ │  │
    │  │ ┌───────────────────────────────────────────────────────────┐ │  │
    │  │ │Image Size Field (Repeater)                                │ │  │
    │  │ │                                                           │ │  │
    │  │ │                      ┌─────────────┐       ┌─────────┐    │ │  │
    │  │ │     Image reference: │           ▼ │  Size:│         │    │ │  │
    │  │ │                      └─────────────┘       └─────────┘    │ │  │
    │  │ │                      ┌─────────────┐       ┌─────────┐    │ │  │
    │  │ │     Image reference: │           ▼ │  Size:│         │    │ │  │
    │  │ │                      └─────────────┘       └─────────┘    │ │  │
    │  │ │                      ┌─────────────┐       ┌─────────┐    │ │  │
    │  │ │     Image reference: │           ▼ │  Size:│         │    │ │  │
    │  │ │                      └─────────────┘       └─────────┘    │ │  │
    │  │ └───────────────────────────────────────────────────────────┘ │  │
    │  │                                                               │  │
    │  └───────────────────────────────────────────────────────────────┘  │
    │                                                                     │
    └─────────────────────────────────────────────────────────────────────┘

    So far, my code (ultra basic, still not working) is the following:

    if (is_admin()){
    	$fieldKey = 'field_5831de130046f';
    
    	function my_acf_load_field($field){
    		global $post;
    		$db_value = get_field('image_size', $post->ID, false);
    		return $field;
    	}
    	add_filter('acf/load_field/key=' . $fieldKey, 'my_acf_load_field', 10, 1);
    }

    First question: If I want to hook the ‘image_reference’ select, the $fieldKey should be the ‘image_reference’ key right?

    Second question: what should be the field I’m asking for with get_field? If my hook (add_filter) is set on ‘image_reference’, I should have get_field(‘image_reference’)?

    I’ve to admit I’m completely lost here…
    Depending the get_field I’m doing (with nothing more than this action), even the “return $field” doesn’t work anymore.

    I tried to contact you in private, but looks like it’s not possible.
    If you want, you can try contacting me on iMessage (same nickname [at] mac.com) or Skype (same nickname).

    Best regards.

  • Ok James,
    I think I made it, but doing my own database request:

    if (is_admin()){
    	$fieldKey = 'field_5831de3000470'; // image_reference
    
    	function my_acf_load_field($field){
    		global $post;
    		global $wpdb;
    		$query = "SELECT * FROM 'myDatabaseName'.'wp_postmeta'  WHERE 'meta_key' LIKE 'taz_shortcodes_%_image_size_%_image_reference' AND 'post_id' = $post->ID";
    		$rows = $wpdb->get_results($query);
    		$field['choices'] = array();
    		foreach ($rows as $row) {
    			$field['choices'][$row->meta_value] = $row->meta_value;
    		}
    		return $field;
    	}
    	add_filter('acf/load_field/key=' . $fieldKey, 'my_acf_load_field', 10, 1);
    }

    Let me know what you’d have done differently.
    Thanks in advance.

  • Hi @tazintosh

    I think it’s good enough. You can also get the flexible content value by using the get_field() function, but it can be too complicated if you have nested repeater.

    Thanks 🙂

  • Thanks for guiding me out to find a working solution James.
    Merry Christmas.

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

The topic ‘Dropdown value saved but not loaded back’ is closed to new replies.