Support

Account

Home Forums Add-ons Repeater Field Fill up repeater fields with category

Solved

Fill up repeater fields with category

  • Hi ACF !

    I’m in trouble right now and i hope that you can help me !

    I would like to create a repeater field that is filled up with my different categories

    For example let’s assume that i have 4 categories in a parent one, like this

    Cities(is the parent one)
    –Amsterdam
    –Lyon
    –Tokyo
    –Berlin

    And i want that when my page is loaded the repeater is pre-filled up with those categories

    Like in my example below

    My example

    I looked up forward so many times those few days that i’m confused, so if this question is already asked, please forgive me and just past me the link if you can ^^’

    Thanks for your answers guys !

  • so guys i come to you with some research !

    I have tried two things, firstly to grab my categories with something like

    $args = array('parent' => 779);$categories = get_categories( $args ); 
    
    						foreach($categories as $category) { 
    						
    						    echo ''.$category->cat_name.'';

    This give me the categories for the current post, and i try to insert it as a row inside my repeater with this inside of my functions.php

    
    $toto = 'images';
    $row = array(
      'image' => 'ahoy'
    );
    add_row('images', $row);

    But it seem to not working, in fact i don’t understand why the add_row is not working at all, i tried a lot of different configuraiton (even the field solution one) but not any ideas about this

    This is wherei am so far ! I keep you in touch if i progress !

  • You need to do this on the load value hook for the repeater

    example:

    
    add_filter('acf/load_value/name=your-repeater-field-here', 'preload_repeater', 20, 3);
    function preload_repeater($value, $post_id, $field) {
      if (!empty($value)) {
        // this repeater already has a value do not change it
        return $value
      }
      // code here to populate repeater
    
      // this is an example of what ACF is expecting for the value
      // the repeater value is an array
      $value= array(
        // each row is a nested array
        array(
          // each nested row array is made up of "field key" => "value pairs"
          'field_012345' => 'value for this sub field',
          'field_123456' => 'value for 2nd sub field',
          // etc....
        ),
        // second row
        array(
          // etc...
        ),
        // etc...
      );
    
      return $value;
    
    }
    
  • Hi !

    Firstly, thanks for your answer, this allow me to move up a bit !

    But i’m still stuck there, here is my code before that i tried to integrate your answer

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );

    Firstly i’ve created a function, and inside of it i ask wp to load only the category that are not belonging to the current post, and that they have the parent’s id as “779”

    `foreach ( $categories as $category ) {
    $value[]=array(
    ‘field_5cd186302ef1c’ => $category->name
    );
    };
    return $value;
    };`

    And then i grab all those terms that i placed in a foreach loop, in order to have all of them inside of my repeater field

    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    And at last i apply the filter that i want, in order to load all of them.

    But this keep adding me some new row once i clicked on update the post, this is quite a bite a problem

    (the entire code create something like this)

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    So i tried to add your answer to my function, and this is creating me something like

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
      if (!empty($value)) {
        // this repeater already has a value do not change it
        return $value
      }
      else{
        foreach (  $categories as $category ) {   
          $value= array(
            array(
            'field_5cd186302ef1c' => $category->name
            )
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 20, 3);

    But not working, any idea to help me ?

    Thanks for your answer john !

  • Hi !

    Firstly, thanks for your answer, this allow me to move up a bit !

    But i’m still stuck there, here is my code before that i tried to integrate your answer

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );

    Firstly i’ve created a function, and inside of it i ask wp to load only the category that are not belonging to the current post, and that they have the parent’s id as “779”

    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };

    And then i grab all those terms that i placed in a foreach loop, in order to have all of them inside of my repeater field

    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    And at last i apply the filter that i want, in order to load all of them.

    But this keep adding me some new row once i clicked on update the post, this is quite a bite a problem

    (the entire code create something like this)

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    So i tried to add your answer to my function, and this is creating me something like

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
      if (!empty($value)) {
        // this repeater already has a value do not change it
        return $value
      }
      else{
        foreach (  $categories as $category ) {   
          $value= array(
            array(
            'field_5cd186302ef1c' => $category->name
            )
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 20, 3);

    But not working, any idea to help me ?

    Thanks for your answer john !

  • Hi !

    Firstly, thanks for your answer, this allow me to move up a bit !

    But i’m still stuck there, here is my code before that i tried to integrate your answer

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );

    Firstly i’ve created a function, and inside of it i ask wp to load only the category that are not belonging to the current post, and that they have the parent’s id as “779”

    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };

    And then i grab all those terms that i placed in a foreach loop, in order to have all of them inside of my repeater field

    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    And at last i apply the filter that i want, in order to load all of them.

    But this keep adding me some new row once i clicked on update the post, this is quite a bite a problem

    (the entire code create something like this)

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    So i tried to add your answer to my function, and this is creating me something like

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
      if (!empty($value)) {
        // this repeater already has a value do not change it
        return $value
      }
      else{
        foreach (  $categories as $category ) {   
          $value= array(
            array(
            'field_5cd186302ef1c' => $category->name
            )
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 20, 3);

    But not working, any idea to help me ? Thanks for your answer john !

  • Hi !

    Firstly, thanks for your answer, this allow me to move up a bit !

    But i’m still stuck there, here is my code before that i tried to integrate your answer

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );

    Firstly i’ve created a function, and inside of it i ask wp to load only the category that are not belonging to the current post, and that they have the parent’s id as “779”

    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };

    And then i grab all those terms that i placed in a foreach loop, in order to have all of them inside of my repeater field

    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    And at last i apply the filter that i want, in order to load all of them.

    But this keep adding me some new row once i clicked on update the post, this is quite a bite a problem

    (the entire code create something like this)

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    So i tried to add your answer to my function, and this is creating me something like

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
      if (!empty($value)) {
        // this repeater already has a value do not change it
        return $value
      }
      else{
        foreach (  $categories as $category ) {   
          $value= array(
            array(
            'field_5cd186302ef1c' => $category->name
            )
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 20, 3);

    But not working, any idea to help me ? Thanks for your answer john !

  • Hi !

    Firstly, thanks for your answer, this allow me to move up a bit !

    But i’m still stuck there, here is my code before that i tried to integrate your answer

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );

    Firstly i’ve created a function, and inside of it i ask wp to load only the category that are not belonging to the current post, and that they have the parent’s id as “779”

    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };

    And then i grab all those terms that i placed in a foreach loop, in order to have all of them inside of my repeater field

    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    And at last i apply the filter that i want, in order to load all of them.

    But this keep adding me some new row once i clicked on update the post, this is quite a bite a problem

    (the entire code create something like this)

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    So i tried to add your answer to my function, and this is creating me something like

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
      if (!empty($value)) {
        // this repeater already has a value do not change it
        return $value
      }
      else{
        foreach (  $categories as $category ) {   
          $value= array(
            array(
            'field_5cd186302ef1c' => $category->name
            )
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 20, 3);

    But not working, any idea to help me ? Thanks for your answer john !

  • Hi !

    Firstly, thanks for your answer, this allow me to move up a bit !

    But i’m still stuck there, here is my code before that i tried to integrate your answer

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );

    Firstly i’ve created a function, and inside of it i ask wp to load only the category that are not belonging to the current post, and that they have the parent’s id as “779”

    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };

    And then i grab all those terms that i placed in a foreach loop, in order to have all of them inside of my repeater field

    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    And at last i apply the filter that i want, in order to load all of them.

    But this keep adding me some new row once i clicked on update the post, this is quite a bite a problem

  • (the entire code create something like this)

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
    foreach (  $categories as $category ) {   
          $value[]=array(
            'field_5cd186302ef1c' => $category->name
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 10, 3);

    So i tried to add your answer to my function, and this is creating me something like

    function my_load_cat($value, $post_id, $field){
        
    $args = array( 'parent' => 779 );
    $post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    $categories = get_categories( $args );
    
      if (!empty($value)) {
        // this repeater already has a value do not change it
        return $value
      }
      else{
        foreach (  $categories as $category ) {   
          $value= array(
            array(
            'field_5cd186302ef1c' => $category->name
            )
          );
      };
      return $value;
    };
    
    }
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'my_load_cat', 20, 3);

    But not working, any idea to help me ? Thanks for your answer john !

  • 
    $value = array();
    foreach (  $categories as $category ) {   
      $value[] = array('field_5cd186302ef1c' => $category->term_id);
    }
    
  • Thanks it help me to solve my problem (sorry for the time to answer you ^^’)

    For those who want my entire code :

    function acf_load_taxonomy_custom_urls_table($value, $post_id, $field){
        
    $args = array( 'taxonomy' => 'ville_formation');
    $post_categories = wp_get_post_terms( get_the_ID(), 'ville_formation', array("fields" => "ids") );
    
    if ( !empty( $post_categories ) ) {
      $args['exclude'] = $post_categories;
    };
    
    $categories = get_terms( $args );
    
    $value=array();
    
      if (empty($value)) {
        foreach (  $categories as $category ) {
            $value [] =  array(
                'field_5cd186302ef1c' => $category->name);
       }     
        
        };
    return $value; 
    };
    add_filter('acf/load_value/name=villes_a_ajouter_III', 'acf_load_taxonomy_custom_urls_table', 10, 3);
Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘Fill up repeater fields with category’ is closed to new replies.