Support

Account

Home Forums Backend Issues (wp-admin) How to dynamically populate repeated with all posts from a custom type? Reply To: How to dynamically populate repeated with all posts from a custom type?

  • let me know if you have any questions

    
    add_filter('acf/load_value/name=add_on_repeater', 'prepopulate_add_on_repeater', 20, 3);
    function prepopulate_add_on_repeater($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value, do not overwrite
        return $value;
      }
      
      // do a query to get all posts of the post type
      $args = array(
        'post_type' => 'add-ons', // your post type
        'posts_per_page' => -1, // -1 get all
        'fields' => 'ids', // return only list of IDs
      );
      $query = new WP_Query($args);
      if (empty($query->posts)) {
        // no posts found
        return $value;
      }
      
      // initialize value, a repeater has an array value
      $value = array();
      
      // loop over posts and built repeater value
      foreach ($query->posts as $add_on_post_id) {
        // each row of the repeater is a nested array
        $value[] = array(
          // you must use field key to set value of sub field of each row
          'field_XXXXXXX' => $add_on_post_id
        );
      }
        
      return $value;
    }