Support

Account

Home Forums Feature Requests Feature Request – Relationship Field Reply To: Feature Request – Relationship Field

  • And what is the problem that this simple code is not present in the core? Please tell me what the difference should be between the “post object” and the “realtionship” field? If there is none at least one field is obsolete!

    I have also already written to ACF support who suggested the same solution as Andrew.

    The code is quite ok, but the add_filter could be dynamic. For this I use this code:

    add_action ('init', 'ACF_set_filter_bidirectional_relationship_update');
    function ACF_set_filter_bidirectional_relationship_update ()
    {
    	
    	$q_acf_fields = new WP_Query (array (	'post_type'			=> 'acf-field',
    														'posts_per_page'	=> -1
    													)
    										  );
    										  
    	if ( $q_acf_fields->have_posts() )
    	{
    		foreach ($q_acf_fields->posts as $acf_field)
    		{
    			
    			$field_settings = unserialize($acf_field->post_content);
    			
    			if ($field_settings['type'] == 'relationship')
    			{
    				add_filter ('acf/update_value/name=' . $acf_field->post_excerpt, 'ACF_bidirectional_relationship_update', 10, 3);
    			}
    		}
    	}
    
    }

    And besides, the post selection of the “relationship field” is also the one that is currently being edited in the selection list, which is yes particularly “useful”!

    That’s why I also immediately added the filter to remove the respective:

    add_filter ( 'acf/fields/relationship/query', 'ACF_relationship_query_filter', 100 , 3);
    function ACF_relationship_query_filter ($args, $field, $post_id )
    {
    	
    	$args ['post__not_in'] = array ($post_id);
    	
    	return $args;
    }