acf_remove_unreadable_grouped_posts() │ ACF 6.8.11

Removes posts the current user can't read from a relational field's search results.

The query already hides posts core doesn't let the user read, but the acf/user_can_read_post filter can deny others. Searches that a site has opted out of perm=readable aren't checked.

Хуков нет.

Возвращает

array. An array with groups, the groups without posts the user can't read, and more, whether there may be more pages of results, judged before any posts were removed.

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

acf_remove_unreadable_grouped_posts( $groups, $args, $field );
$groups(массив) (обязательный)
The posts from acf_get_grouped_posts(), grouped by post type label.
$args(массив) (обязательный)
The query args.
$field(массив) (обязательный)
The relational field being searched.

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

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

Код acf_remove_unreadable_grouped_posts() ACF 6.8.11

function acf_remove_unreadable_grouped_posts( $groups, $args, $field ) {
	$groups   = acf_get_array( $groups );
	$per_page = isset( $args['posts_per_page'] ) ? (int) $args['posts_per_page'] : 0;
	$more     = $per_page > 0 && array_sum( array_map( 'count', $groups ) ) >= $per_page;

	if ( isset( $args['perm'] ) && 'readable' === $args['perm'] ) {
		foreach ( array_keys( $groups ) as $label ) {
			$groups[ $label ] = array_filter(
				$groups[ $label ],
				function ( $post ) use ( $field ) {
					return acf_current_user_can_read_post( $post, $field );
				}
			);

			if ( empty( $groups[ $label ] ) ) {
				unset( $groups[ $label ] );
			}
		}
	}

	return array(
		'groups' => $groups,
		'more'   => $more,
	);
}