WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

acf_field_relationship::get_ajax_query() public ACF 5.0.9

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

@type function

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

Возвращает

(Массив).

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

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

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

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

Код acf_field_relationship::get_ajax_query() ACF 5.10.2

function get_ajax_query( $options = array() ) {

	// defaults
	$options = wp_parse_args(
		$options,
		array(
			'post_id'   => 0,
			's'         => '',
			'field_key' => '',
			'paged'     => 1,
			'post_type' => '',
			'taxonomy'  => '',
		)
	);

	// 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']          = intval( $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( $options['post_type'] ) ) {

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

	} elseif ( ! empty( $field['post_type'] ) ) {

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

	} else {

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

	}

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

		// vars
		$term = acf_decode_taxonomy_term( $options['taxonomy'] );

		// tax query
		$args['tax_query'] = array();

		// append
		$args['tax_query'][] = array(
			'taxonomy' => $term['taxonomy'],
			'field'    => 'slug',
			'terms'    => $term['term'],
		);

	} elseif ( ! empty( $field['taxonomy'] ) ) {

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

		// append to $args
		$args['tax_query'] = array(
			'relation' => 'OR',
		);

		// 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/relationship/query', $args, $field, $options['post_id'] );
	$args = apply_filters( 'acf/fields/relationship/query/name=' . $field['name'], $args, $field, $options['post_id'] );
	$args = apply_filters( 'acf/fields/relationship/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'] );

		}

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

	}

	// add as optgroup or results
	if ( count( $args['post_type'] ) == 1 ) {

		$results = $results[0]['children'];

	}

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

	// return
	return $response;

}