When using the relationship ACF field, this snippet will hide the current post from being listed in a list of the specified CPT.
In this example, the CPT is ‘Events’ and ‘related_events’ is the name of the ACF Relationship field.
$args
uses the WP_Query arguments to find the overall list.
$field
is the field name.
$post
is the current post ID being edited.
function exclude_current_event( $args, $field, $post ) {
$args['post__not_in'] = array( $post );
return $args;
}
add_filter( 'acf/fields/relationship/query/name=related_events', 'exclude_current_event', 10, 3 );
Additional Reading:
- ACF Docs: Relationship
- ACF Docs: Relationship Query