acf_field_taxonomy::get_ajax_query │ public │ ACF 5.0.9
This function will return an array of data formatted for use in a select2 AJAX response
Метод класса: acf_field_taxonomy{}
Возвращает
(Массив).
Использование
$acf_field_taxonomy = new acf_field_taxonomy();
$acf_field_taxonomy->get_ajax_query( $options );
- $options
- .
По умолчанию: array()
Список изменений
Код acf_field_taxonomy::get_ajax_query() acf field taxonomy::get ajax query
ACF 6.4.2
function get_ajax_query( $options = array() ) {
$options = acf_parse_args(
$options,
array(
'post_id' => 0,
's' => '',
'field_key' => '',
'term_id' => '',
'include' => '',
'paged' => 1,
)
);
$field = acf_get_field( $options['field_key'] );
if ( ! $field ) {
return false;
}
// if options include isset, then we are loading a specific term.
if ( ! empty( $options['include'] ) ) {
$options['term_id'] = $options['include'];
// paged should be 1.
$options['paged'] = 1;
}
// Bail early if taxonomy does not exist.
if ( ! taxonomy_exists( $field['taxonomy'] ) ) {
return false;
}
$results = array();
$is_hierarchical = is_taxonomy_hierarchical( $field['taxonomy'] );
$is_pagination = ( $options['paged'] > 0 );
$is_search = false;
$limit = 20;
$offset = 20 * ( $options['paged'] - 1 );
$args = array(
'taxonomy' => $field['taxonomy'],
'hide_empty' => false,
);
// Don't bother for hierarchial terms, we will need to load all terms anyway.
if ( $is_pagination && ! $is_hierarchical ) {
$args['number'] = $limit;
$args['offset'] = $offset;
}
// search
if ( $options['s'] !== '' ) {
// strip slashes (search may be integer)
$s = wp_unslash( strval( $options['s'] ) );
$args['search'] = isset( $options['term_id'] ) && $options['term_id'] ? '' : $s;
$is_search = true;
}
$args = apply_filters( 'acf/fields/taxonomy/query', $args, $field, $options['post_id'] );
if ( ! empty( $options['include'] ) ) {
// Limit search to a specific id if one is provided.
$args['include'] = $options['include'];
}
$terms = acf_get_terms( $args );
// Sort hierachial.
if ( $is_hierarchical ) {
$limit = acf_maybe_get( $args, 'number', $limit );
$offset = acf_maybe_get( $args, 'offset', $offset );
$parent = acf_maybe_get( $args, 'parent', 0 );
$parent = acf_maybe_get( $args, 'child_of', $parent );
// This will fail if a search has taken place because parents wont exist.
if ( ! $is_search ) {
$ordered_terms = _get_term_children( $parent, $terms, $field['taxonomy'] );
// Check for empty array. Possible if parent did not exist within original data.
if ( ! empty( $ordered_terms ) ) {
$terms = $ordered_terms;
}
}
// Fake pagination.
if ( $is_pagination && ! $options['include'] ) {
$terms = array_slice( $terms, $offset, $limit );
}
}
// Append to r.
foreach ( $terms as $term ) {
// Add to json.
$results[] = array(
'id' => $term->term_id,
'text' => $this->get_term_title( $term, $field, $options['post_id'], true ),
);
}
$response = array(
'results' => $results,
'limit' => $limit,
);
return $response;
}