Support

Account

Home Forums General Issues a new select field with data from query_args Reply To: a new select field with data from query_args

  • 
    add_fitler('acf/load/field/key=field_XXXXXX', 'my_dynamic_select_field', 20);
    function my_dynamic_select_field($field) {
      $choices = array();
      args = array(
        'post_status'    => 'publish',
        'posts_per_page' => 6,
        'meta_key' => '_wp_page_template',
        'meta_value' => 'page_people3rd.php'
      );
      $query = new WP_Query($args);
      if (count($query->posts)) {
        foreach ($query->posts as $post) {
          $choices[$post->ID] = $post->post_title;
        }
      }
      $field['choices'] = $choices;
      return $field;
    }
    

    You could also yous a post object field or a relationship field and then use https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/ or https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ to alter the query done to populate the page.

    
    function my_relationship_query( $args, $field, $post_id ) {
    	
        // only show children of the current post being edited
        $args['meta_key'] = '_wp_page_template';
        $args['meta_value'] = 'page_people3rd.php';
    	
    	// return
        return $args;
        
    }
    
    // filter for every field
    add_filter('acf/fields/relationship/query/key=field_XXXXXXX', 'my_relationship_query', 10, 3);