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

acf_get_grouped_posts() ACF 5.0.0

This function will return all posts grouped by post_type This is handy for select settings

Хуков нет.

Возвращает

(Массив).

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

acf_get_grouped_posts( $args );
$args (обязательный)
-

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

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

Код acf_get_grouped_posts() ACF 5.9.1

<?php
function acf_get_grouped_posts( $args ) {
	
	// vars
	$data = array();
	
	
	// defaults
	$args = wp_parse_args( $args, array(
		'posts_per_page'			=> -1,
		'paged'						=> 0,
		'post_type'					=> 'post',
		'orderby'					=> 'menu_order title',
		'order'						=> 'ASC',
		'post_status'				=> 'any',
		'suppress_filters'			=> false,
		'update_post_meta_cache'	=> false,
	));

	
	// find array of post_type
	$post_types = acf_get_array( $args['post_type'] );
	$post_types_labels = acf_get_pretty_post_types($post_types);
	$is_single_post_type = ( count($post_types) == 1 );
	
	
	// attachment doesn't work if it is the only item in an array
	if( $is_single_post_type ) {
		$args['post_type'] = reset($post_types);
	}
	
	
	// add filter to orderby post type
	if( !$is_single_post_type ) {
		add_filter('posts_orderby', '_acf_orderby_post_type', 10, 2);
	}
	
	
	// get posts
	$posts = get_posts( $args );
	
	
	// remove this filter (only once)
	if( !$is_single_post_type ) {
		remove_filter('posts_orderby', '_acf_orderby_post_type', 10, 2);
	}
	
	
	// loop
	foreach( $post_types as $post_type ) {
		
		// vars
		$this_posts = array();
		$this_group = array();
		
		
		// populate $this_posts
		foreach( $posts as $post ) {
			if( $post->post_type == $post_type ) {
				$this_posts[] = $post;
			}
		}
		
		
		// bail early if no posts for this post type
		if( empty($this_posts) ) continue;
		
		
		// sort into hierachial order!
		// this will fail if a search has taken place because parents wont exist
		if( is_post_type_hierarchical($post_type) && empty($args['s'])) {
			
			// vars
			$post_id = $this_posts[0]->ID;
			$parent_id = acf_maybe_get($args, 'post_parent', 0);
			$offset = 0;
			$length = count($this_posts);
			
			
			// get all posts from this post type
			$all_posts = get_posts(array_merge($args, array(
				'posts_per_page'	=> -1,
				'paged'				=> 0,
				'post_type'			=> $post_type
			)));
			
			
			// find starting point (offset)
			foreach( $all_posts as $i => $post ) {
				if( $post->ID == $post_id ) {
					$offset = $i;
					break;
				}
			}
			
			
			// order posts
			$ordered_posts = get_page_children($parent_id, $all_posts);
			
			
			// compare aray lengths
			// if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
			// this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters) 
			if( count($ordered_posts) == count($all_posts) ) {
				$this_posts = array_slice($ordered_posts, $offset, $length);
			}
			
		}
		
		
		// populate $this_posts
		foreach( $this_posts as $post ) {
			$this_group[ $post->ID ] = $post;
		}
		
		
		// group by post type
		$label = $post_types_labels[ $post_type ];
		$data[ $label ] = $this_group;
					
	}
	
	
	// return
	return $data;
	
}