Support

Account

Home Forums ACF PRO dynamically updating a select field inside a plugin

Helping

dynamically updating a select field inside a plugin

  • Hi,

    I’ve managed to dynamically update a select field using functions php but I’m struggling to do the same inside a plugin. I would like the filter to run on page load but nothing happens as I think the $field is returning empty.

    function outcome_admin_page() {
    
      if (!current_user_can('manage_options'))  {
        wp_die( __('You do not have sufficient permissions to access this page.')    );
      }
    
      echo '<div class="wrap">';
    
      echo '<h2>CPT Merged Data Table</h2>';
    
      acf_form_head();
    
      acf_form(array(
            'post_id'		=> 'new_post',
            'post_title'	=> true,
            'new_post'		=> array(
              'post_type'		=> 'outcome',
              'post_status'		=> 'publish'
            ),
            'submit_value'		=> 'Create a new outcome'
          ));
    
          linked_outcome_add_iframe( $field );
    }
    
    function linked_outcome_add_iframe( $field ){
    
        echo $field;
        $outcome_posts = get_posts(
          array(
              'posts_per_page' => -1,
              'post_type' => 'outcome',
              'order' => 'DESC',
              'orderby' => 'title'
    
          )
      );
      if(!empty($outcome_posts)){
        foreach($outcome_posts as $outcome_post){
    
          $variable = get_field('outcome_post_title', $outcome_post->ID);
          echo $variable;
          $field['choices'][$variable] = $variable;
        }
      }
    
        return $field;
    }
    add_filter('acf/load_field/key=field_5bbe31e6eb5fb', 'linked_outcome_add_iframe');

    I’m not quite sure how to accomplish this – any help would be most appreciated.

    Thanks,
    D

  • Would it be possible to just do in a standard page template also?

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

The topic ‘dynamically updating a select field inside a plugin’ is closed to new replies.