Overview
Change whether a user can read a post referenced by a Relationship, Post Object, or Page Link field.
Description
ACF checks whether a user can read a post before showing its title in a Relationship, Post Object, or Page Link field (including search results), saving a newly selected post, or including it in an ACF Block’s values. Posts that fail the check are left out of search results, and selected ones show as “(Restricted item)”. By default, ACF follows WordPress: published and publicly viewable posts can be read by anyone, and other posts need the read_post capability. There are two stricter cases:
- Posts of non-public post types, such as WooCommerce coupons, are only readable by users who can edit the post, or, if it’s published, by users who can open that post type’s list in the admin. This doesn’t apply when the field’s “Filter by Post Type” setting includes the post type.
- Revisions are only readable by users who can edit them.
Use this filter to change the result, for example when a membership plugin grants access to individual posts.
Parameters
apply_filters( 'acf/user_can_read_post', $can_read, $post, $field, $user );
$can_read
(bool) Whether the user can read the post.
$post
(WP_Post|null) The post object, or null if the post doesn’t exist.
$field
(array) The field referencing the post, if any.
$user
(WP_User) The user being checked.
Example
Let members read posts granted to them
add_filter( 'acf/user_can_read_post', function ( $can_read, $post, $field, $user ) {
if ( $post && my_membership_user_has_access( $user->ID, $post->ID ) ) {
return true;
}
return $can_read;
}, 10, 4 );
Changelog
- Added in version 6.8.11
Notes
- Check
$userrather than the current user. While an ACF Block renders on the front end,$useris the author of the post the block is saved in. - Returning
falseremoves a post from field search results. Returningtruedoesn’t add posts the search query has already left out: other users’ posts with unreadable statuses, and other users’ posts of non-public post types the field doesn’t list. Use theacf/fields/relational/unreadable_post_statusesfilter to include posts with those statuses.
Supercharge Your Website With Premium Features Using ACF PRO
Speed up your workflow and unlock features to better develop websites using ACF Blocks and Options Pages, with the Flexible Content, Repeater, Clone, Gallery Fields & More.
Related
- Filters: acf/fields/relational/unreadable_post_statuses
- Filters: acf/fields/relational/hide_unreadable_posts
- Filters: acf/fields/relationship/result
- Filters: acf/fields/allow_append_choices
- Filters: acf/bidirectional/can_update_target