acf_get_posts()ACF 5.1.5

acf_get_posts

Similar to the get_posts() function but with extra functionality.

Returns

Array.

Usage

acf_get_posts( $args );
$args(array)
The query args.
Default: array()

Changelog

Since 5.1.5 Introduced.

acf_get_posts() code ACF 6.8.8

function acf_get_posts( $args = array() ) {

	// Vars.
	$posts = array();

	// Apply default args.
	$args = wp_parse_args(
		$args,
		array(
			'posts_per_page'         => -1,
			'post_type'              => '',
			'post_status'            => 'any',
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		)
	);

	// Avoid default 'post' post_type by providing all public types.
	if ( ! $args['post_type'] ) {
		$args['post_type'] = acf_get_post_types();
	}

	if ( ! $args['post_status'] ) {
		$args['post_status'] = acf_get_post_stati();
	}

	// Check if specific post IDs have been provided.
	if ( $args['post__in'] ) {

		// Clean value into an array of IDs.
		$args['post__in'] = array_map( 'intval', acf_array( $args['post__in'] ) );
	}

	/**
	 * Filters the args used in `acf_get_posts()` that are passed to `get_posts()`.
	 *
	 * @since 6.1.7
	 *
	 * @param array $args The args passed to `get_posts()`.
	 */
	$args = apply_filters( 'acf/acf_get_posts/args', $args );

	// Query posts.
	$posts = get_posts( $args );

	// Remove any potential empty results.
	$posts = array_filter( $posts );

	// Manually order results.
	if ( $posts && $args['post__in'] ) {
		$order = array();
		foreach ( $posts as $i => $post ) {
			$order[ $i ] = array_search( $post->ID, $args['post__in'] );
		}
		array_multisort( $order, $posts );
	}

	/**
	 * Filters the results found in the `acf_get_posts()` function.
	 *
	 * @since 6.1.7
	 *
	 * @param array $posts The results from the `get_posts()` call.
	 */
	return apply_filters( 'acf/acf_get_posts/results', $posts );
}