acf_user_can_read_post() │ ACF 6.8.11

Checks whether a user can read a post referenced by a relational field.

Matches WordPress core: published posts can be read by anyone (as in the REST API, and as the relational field search already shows them), and other posts need to be publicly viewable or need the read_post capability (which needs edit access for other users' drafts).

Posts of post types that aren't public, such as WooCommerce coupons, can only be seen in the admin. Published ones are readable if the field's "Filter by Post Type" setting lists their post type, or the user can open the post type's list of posts in the admin, which shows their titles. Others need the user to be able to edit them. Revisions are never public, so they need edit access, which for a revision means edit access to its parent.

Unlike acf_rest_reference_is_exposable(), this doesn't require the post type to be shown in the REST API, because these fields can reference any post type in the editor.

Хуки из функции

Возвращает

boolean.

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

acf_user_can_read_post( $user, $post, $field );
$user(int|WP_User) (обязательный)
The user ID or object. 0 for visitors who aren't logged in.
$post(int|WP_Post) (обязательный)
The post ID or object.
$field(массив)
The field referencing the post.
По умолчанию: array()

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

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

Код acf_user_can_read_post() ACF 6.8.11

function acf_user_can_read_post( $user, $post, $field = array() ) {
	$user = $user instanceof WP_User ? $user : new WP_User( (int) $user );
	$post = get_post( $post );

	if ( ! $post ) {
		$can_read = false;
	} elseif ( 'revision' === $post->post_type ) {
		$can_read = user_can( $user, 'edit_post', $post->ID );
	} elseif ( ! is_post_type_viewable( $post->post_type ) && ! in_array( $post->post_type, acf_get_array( $field['post_type'] ?? array() ), true ) ) {
		$can_read = ( 'publish' === get_post_status( $post ) && _acf_user_can_list_post_type( $user, $post->post_type ) )
			|| user_can( $user, 'edit_post', $post->ID );
	} else {
		$can_read = 'publish' === get_post_status( $post )
			|| is_post_publicly_viewable( $post )
			|| user_can( $user, 'read_post', $post->ID );
	}

	/**
	 * Filters whether a user can read a post referenced by a relational field.
	 *
	 * The user is usually the current user. While an ACF Block saved in a post or synced pattern renders,
	 * other than in a preview, it's the author of that post, so check `$user` rather than the current user.
	 *
	 * Relational field searches leave out other users' posts the user can't read before this filter runs,
	 * so returning true doesn't add them to search results. Use the
	 * `acf/fields/relational/unreadable_post_statuses` filter to include them.
	 *
	 * @since 6.8.11
	 *
	 * @param boolean      $can_read Whether the user can read the post.
	 * @param WP_Post|null $post     The post object, or null if the post doesn't exist.
	 * @param array        $field    The field referencing the post, if any.
	 * @param WP_User      $user     The user.
	 */
	return (bool) apply_filters( 'acf/user_can_read_post', $can_read, $post, $field, $user );
}