acf_field_post_object::get_ajax_query()publicACF 5.0.9

get_ajax_query

This function will return an array of data formatted for use in a select2 AJAX response

@type    function

{} Это метод класса: acf_field_post_object{}

Возвращает

(Массив).

Использование

$acf_field_post_object = new acf_field_post_object();
$acf_field_post_object->get_ajax_query( $options );
$options **
-
По умолчанию: array()

Список изменений

С версии 5.0.9 Введена.

Код acf_field_post_object::get_ajax_query() ACF 5.10.2

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;
	}

	// vars
	$results   = array();
	$args      = array();
	$s         = false;
	$is_search = false;

	// paged
	$args['posts_per_page'] = 20;
	$args['paged']          = $options['paged'];

	// search
	if ( $options['s'] !== '' ) {

		// strip slashes (search may be integer)
		$s = wp_unslash( strval( $options['s'] ) );

		// update vars
		$args['s'] = $s;
		$is_search = true;

	}

	// post_type
	if ( ! empty( $field['post_type'] ) ) {

		$args['post_type'] = acf_get_array( $field['post_type'] );

	} else {

		$args['post_type'] = acf_get_post_types();

	}

	// taxonomy
	if ( ! empty( $field['taxonomy'] ) ) {

		// vars
		$terms = acf_decode_taxonomy_terms( $field['taxonomy'] );

		// append to $args
		$args['tax_query'] = array();

		// now create the tax queries
		foreach ( $terms as $k => $v ) {

			$args['tax_query'][] = array(
				'taxonomy' => $k,
				'field'    => 'slug',
				'terms'    => $v,
			);

		}
	}

	// filters
	$args = apply_filters( 'acf/fields/post_object/query', $args, $field, $options['post_id'] );
	$args = apply_filters( 'acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id'] );
	$args = apply_filters( 'acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id'] );

	// get posts grouped by post type
	$groups = acf_get_grouped_posts( $args );

	// bail early if no posts
	if ( empty( $groups ) ) {
		return false;
	}

	// loop
	foreach ( array_keys( $groups ) as $group_title ) {

		// vars
		$posts = acf_extract_var( $groups, $group_title );

		// data
		$data = array(
			'text'     => $group_title,
			'children' => array(),
		);

		// convert post objects to post titles
		foreach ( array_keys( $posts ) as $post_id ) {

			$posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'], $is_search );

		}

		// order posts by search
		if ( $is_search && empty( $args['orderby'] ) && isset( $args['s'] ) ) {

			$posts = acf_order_by_search( $posts, $args['s'] );

		}

		// append to $data
		foreach ( array_keys( $posts ) as $post_id ) {

			$data['children'][] = $this->get_post_result( $post_id, $posts[ $post_id ] );

		}

		// append to $results
		$results[] = $data;

	}

	// optgroup or single
	$post_type = acf_get_array( $args['post_type'] );
	if ( count( $post_type ) == 1 ) {
		$results = $results[0]['children'];
	}

	// vars
	$response = array(
		'results' => $results,
		'limit'   => $args['posts_per_page'],
	);

	// return
	return $response;

}