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?

  • 
    add_filter('acf/load_value/name=post_object_repeater', 'prepopulate_post_object_with_all_posts', 20, 3);
    function prepopulate_post_object_with_all_posts($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;
      }
      
      $value = $query->posts;
        
      return $value;
    }