Support

Account

Home Forums ACF PRO load_fields with cloned fields

Solved

load_fields with cloned fields

  • Hi,

    I’ve been aware of the cloned fields field type, but only started using it on a recent project.

    I ran in to some issues with trying to populate a select field (that was part of the cloned fields) with options from looping over a flexible content field.

    Basically looping over flexible content rows checking for a particular layout and, if found, adding a text value to the choices of the cloned select.

    I was wondering if anyone else had run in to similar issues at all?

    Also I had conditional logic so that (pre populated) select wasn’t always showing, but when I added the load_fields filter it reset the controlling field to none, so would always hide all fields.

    I appreciate without an example this is pretty abstract, so may need an example of code…I just wanted to find out if anyone had had similar issues at all.

    Happy to extract code examples if anyone wants them!

    Thanks in advance,
    Tom

  • The code that you’re using to populate the select field choices would probably be helpful in this case.

  • Yeah sorry, I should’ve included anyway! The code is as follows:

    function acf_load_anchor_points( $field ) {
        
        $post_id = get_the_ID();
    
        // if has rows
        if (have_rows('blocks', $post_id)):
            
            // reset choices
            $field['choices'] = array();
            
            // while has rows
            while (have_rows('blocks', $post_id)): the_row();
                
                if (get_row_layout() ==  'anchor_point'):
                    $title = get_sub_field('title');
                    $slug = sanitize_title($title);
                    $field['choices'][ $slug ] = $title;
                endif;
                
            endwhile;
            
        endif;
    
        // return the field
        return $field;
        
    }
    add_filter('acf/load_field/key=field_57a9b1c80a552', 'acf_load_anchor_points');
  • It looks like it should be working. You need to test to see if this $post_id = get_the_ID(); is actually the current post ID. Right after that add echo '<br>',$post_id,'<br>'; and make sure that you’re trying to get the values from the correct post.

  • That’s the thing, it did work, then bombed on memory exhaustion.

    I echoed the options and ID to the instructions field and all was correct. wasnt quote sure what might be the issue!

  • You didn’t mention that the problem was running out of memory.

    It could be that your have_rows() loop is creating an infinite loop, this can happen when using ACF function to load values inside of filters for other ACF functions.

    What you might have to do is use get_post_meta() instead of the ACF functions.

    For example

    
    $layouts = get_post_meta($post_id, 'blocks', true);
    

    will return an array of layouts, something like this.

    
    $layouts = get_post_meta($post_id, 'blocks', true);
    if (count($layouts)) {
      $field['choices'] = array();
      for ($i=0; $i<count($layouts); $i++) {
        if ($layouts[$i]) == 'anchor_point') {
          $title = get_post_meta($post_id, 'block_'.$i.'_title', true);
          $slug = sanitize_title($title);
          $field['choices'][ $slug ] = $title;
        } // end if layouts[$i] is anchor point
      } // end for layouts
    } // end if layouts
    
  • Ah apologies, just read up and realised I didn’t. It had been a long week!

    That sounds like it could be it.
    I’ll give that a whirl. Thanks for your time and help.

  • Thanks for everything so far John, I wonder if you had any thoughts on the cloned field aspect? Should loading field values work in this way without issues that you can think of?

    As I understand it the field id is the same across the field groups that you clone them to? So would the field loading/population happen across the board?

  • I just ran into a similar issue on the only site where I am using cloned fields. In one of my acf/save_post filters I’m getting the sub fields of a repeater and for some reason this is causing an infinite loop that timing out the server. I haven’t got a clue what’s going on with it because it is just one repeater of many, the only difference is that it’s in a nested repeater in a flexible content field that is a clone.

    I love ACF, but I may not use cloned fields again. In my case I can skip the code and just not bother with this because what’s being done is not vital.

    As far as loading fields that way without issue, going to be honest, I don’t have a clue at this point. The best advice that I can give you is to look in the database and find out exactly how your repeater inside a clone field is stored in the database, what meta keys are used and what information is stored in them.

  • Yeah this is my first outing with cloned fields too. Seems like a great feature, but one that will open up issues for more complicated setups. Mine are nested within flexible content fields and other spots so I’ve held off for now, but am going to try the other loop method you mentioned for now!

    Thanks again!

  • Although just in case anyone else should view this, there is a typo on your response, an extra closing bracket within the if statement.

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

The topic ‘load_fields with cloned fields’ is closed to new replies.