acf_field_select::get_ajax_query
get_ajax_query
This function will return an array of data formatted for use in a select2 AJAX response
Method of the class: acf_field_select{}
No Hooks.
Returns
(Array).
Usage
$acf_field_select = new acf_field_select(); $acf_field_select->get_ajax_query( $options );
- $options
- .
Default:array()
Changelog
| Since 5.0.9 | Introduced. |
acf_field_select::get_ajax_query() acf field select::get ajax query code ACF 6.0.4
function get_ajax_query( $options = array() ) {
// defaults
$options = acf_parse_args(
$options,
array(
'post_id' => 0,
's' => '',
'field_key' => '',
'paged' => 1,
)
);
// load field
$field = acf_get_field( $options['field_key'] );
if ( ! $field ) {
return false;
}
// get choices
$choices = acf_get_array( $field['choices'] );
if ( empty( $field['choices'] ) ) {
return false;
}
// vars
$results = array();
$s = null;
// search
if ( $options['s'] !== '' ) {
// strip slashes (search may be integer)
$s = strval( $options['s'] );
$s = wp_unslash( $s );
}
// loop
foreach ( $field['choices'] as $k => $v ) {
// ensure $v is a string
$v = strval( $v );
// if searching, but doesn't exist
if ( is_string( $s ) && stripos( $v, $s ) === false ) {
continue;
}
// append
$results[] = array(
'id' => $k,
'text' => $v,
);
}
// vars
$response = array(
'results' => $results,
);
// return
return $response;
}