acf_send_ajax_results()ACF 5.4.0

acf_send_ajax_results

This function will print JSON data for a Select2 AJAX query

No Hooks.

Returns

null. Nothing (null).

Usage

acf_send_ajax_results( $response );
$response(required)
.

Changelog

Since 5.4.0 Introduced.

acf_send_ajax_results() code ACF 6.8.8

function acf_send_ajax_results( $response ) {

	// validate
	$response = wp_parse_args(
		$response,
		array(
			'results' => array(),
			'more'    => false,
			'limit'   => 0,
		)
	);

	// limit
	if ( $response['limit'] && $response['results'] ) {

		// vars
		$total = 0;

		foreach ( $response['results'] as $result ) {

			// parent
			++$total;

			// children
			if ( ! empty( $result['children'] ) ) {
				$total += count( $result['children'] );
			}
		}

		// calc
		if ( $total >= $response['limit'] ) {
			$response['more'] = true;
		}
	}

	// return
	wp_send_json( $response );
}