acf_get_grouped_posts() │ ACF 5.0.0
acf_get_grouped_posts
This function will return all posts grouped by post_type This is handy for select settings
Хуков нет.
Возвращает
array.
Использование
acf_get_grouped_posts( $args );
- $args(массив) (обязательный)
- The query arguments.
Список изменений
| С версии 5.0.0 | Введена. |
Код acf_get_grouped_posts() acf get grouped posts ACF 6.8.8
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,
)
);
// Narrow anonymous callers to publicly-viewable types/statuses.
if ( ! is_user_logged_in() ) {
$requested_post_types = acf_get_array( $args['post_type'] );
if ( in_array( 'any', $requested_post_types, true ) ) {
$requested_post_types = get_post_types();
}
$viewable_post_types = array_values( array_filter( $requested_post_types, 'is_post_type_viewable' ) );
if ( empty( $viewable_post_types ) ) {
return $data;
}
$requested_post_statuses = acf_get_array( $args['post_status'] );
$public_post_statuses = array_values( array_filter( get_post_stati(), 'is_post_status_viewable' ) );
if ( empty( $requested_post_statuses ) ) {
return $data;
}
if ( in_array( 'any', $requested_post_statuses, true ) ) {
$viewable_post_statuses = $public_post_statuses;
if ( in_array( 'attachment', $viewable_post_types, true ) ) {
$viewable_post_statuses[] = 'inherit';
}
} else {
$viewable_post_statuses = array_values( array_intersect( $requested_post_statuses, $public_post_statuses ) );
if ( in_array( 'attachment', $viewable_post_types, true ) && in_array( 'inherit', $requested_post_statuses, true ) ) {
$viewable_post_statuses[] = 'inherit';
}
}
if ( empty( $viewable_post_statuses ) ) {
return $data;
}
$args['post_type'] = $viewable_post_types;
$args['post_status'] = array_values( array_unique( $viewable_post_statuses ) );
}
// find array of post_type
$post_types = acf_get_array( $args['post_type'] );
$is_single_post_type = ( count( $post_types ) === 1 );
// WordPress 6.8+ sorts post_type arrays for cache key generation
// We need to use the same sorted order when processing results
if (
! $is_single_post_type &&
$args['posts_per_page'] !== -1 &&
version_compare( get_bloginfo( 'version' ), '6.8', '>=' )
) {
sort( $post_types );
}
$post_types_labels = acf_get_pretty_post_types( $post_types );
// 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;
}