acf_remove_unreadable_formatted_post_ids()
Removes posts that can't be read from a relational field's value before it's formatted.
Applies to values formatted while rendering an ACF Block by default. Block data is stored in the post content, which anyone who can edit the post controls, and the block editor renders previews from data that hasn't been saved yet, so selections in it are never checked when they're saved.
Previews are checked against the current user. Other blocks are checked against the author of the post or synced pattern they're saved in, so posts that author picked show to every visitor but posts they couldn't have picked don't. Blocks that aren't saved in either are checked against the current user when rendered in the admin, by AJAX or by a REST API endpoint, and aren't checked on the front end, where they're in places like template parts and widgets that only administrators can edit. See _acf_get_block_reader().
The acf/fields/relational/hide_unreadable_posts filter can apply this to other values too, or turn it off.
Хуки из функции
Возвращает
mixed. The value without posts that can't be read.
Использование
acf_remove_unreadable_formatted_post_ids( $value, $post_id, $field );
- $value(разное) (обязательный)
- The value: an ID, or an array of IDs (and Page Link URLs).
- $post_id(int|строка) (обязательный)
- The ACF post ID the value is formatted for.
- $field(массив) (обязательный)
- The field array.
Список изменений
| С версии 6.8.11 | Введена. |
Код acf_remove_unreadable_formatted_post_ids() acf remove unreadable formatted post ids ACF 6.8.11
function acf_remove_unreadable_formatted_post_ids( $value, $post_id, $field ) {
if ( empty( $value ) ) {
return $value;
}
$user = wp_get_current_user();
$hide = false;
if ( is_string( $post_id ) && 0 === strpos( $post_id, 'block_' ) ) {
// Set while an ACF Block renders: false when it isn't checked, otherwise the user to check.
$reader = acf_get_data( 'acf_block_reader' );
$hide = false !== $reader;
$user = $reader instanceof WP_User ? $reader : $user;
}
/**
* Filters whether a relational field's formatted value leaves out posts that can't be read.
*
* True by default for values formatted while rendering an ACF Block, such as by get_field() in a block
* template, except for blocks on the front end that aren't saved in a post or synced pattern. Return
* true to also leave them out elsewhere, such as from get_field() in theme templates, or false to show
* them in blocks again. Values other than those of blocks are checked against the current user.
* Visitors who aren't logged in can only read published posts and posts with a public status.
*
* @since 6.8.11
*
* @param boolean $hide Whether to leave out posts that can't be read.
* @param integer|string $post_id The ACF post ID the value is formatted for.
* @param array $field The field array.
*/
if ( ! apply_filters( 'acf/fields/relational/hide_unreadable_posts', $hide, $post_id, $field ) ) {
return $value;
}
$is_readable = function ( $id ) use ( $user, $field ) {
// Page Link URLs aren't post IDs, so they're kept. Other fields convert values like `123x` to post IDs.
if ( ! is_numeric( $id ) ) {
return 'page_link' === $field['type'];
}
return acf_user_can_read_post( $user, (int) $id, $field );
};
if ( ! is_array( $value ) ) {
return $is_readable( $value ) ? $value : '';
}
_prime_post_caches( array_map( 'intval', array_filter( $value, 'is_numeric' ) ), false, false );
$value = array_filter( $value, $is_readable );
return acf_is_sequential_array( $value ) ? array_values( $value ) : $value;
}