Stop ACF Fields Linking a Post to Itself
Removes the post being edited from the choices of ACF relationship and post object fields.
Opens WPIDE on
Code
$wpide_acf_exclude_self = function ( $args, $field, $post_id ) {
if ( is_numeric( $post_id ) && $post_id ) {
$args['post__not_in'] = array_merge( (array) ( $args['post__not_in'] ?? array() ), array( (int) $post_id ) );
}
return $args;
};
add_filter( 'acf/fields/relationship/query', $wpide_acf_exclude_self, 10, 3 );
add_filter( 'acf/fields/post_object/query', $wpide_acf_exclude_self, 10, 3 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.