acf_field_page_link::get_postspublicACF 5.0.0

This function will return an array of posts for a given field value

Method of the class: acf_field_page_link{}

No Hooks.

Returns

$value.

Usage

$acf_field_page_link = new acf_field_page_link();
$acf_field_page_link->get_posts( $value, $field );
$value(required)
.
$field(required)
.

Changelog

Since 5.0.0 Introduced.

acf_field_page_link::get_posts() code ACF 6.8.8

function get_posts( $value, $field ) {

	// force value to array
	$value = acf_get_array( $value );

	// get selected post ID's
	$post__in = array();

	foreach ( $value as $k => $v ) {
		if ( is_numeric( $v ) ) {

			// append to $post__in
			$post__in[] = (int) $v;
		}
	}

	// bail early if no posts
	if ( empty( $post__in ) ) {
		return $value;
	}

	// get posts
	$posts = acf_get_posts(
		array(
			'post__in'  => $post__in,
			'post_type' => $field['post_type'],
		)
	);

	// override value with post
	$return = array();

	// append to $return
	foreach ( $value as $k => $v ) {
		if ( is_numeric( $v ) ) {

			// extract first post
			$post = array_shift( $posts );

			// append
			if ( $post ) {
				$return[] = $post;
			}
		} else {
			$return[] = $v;
		}
	}

	// return
	return $return;
}