Support

Account

Home Forums Feature Requests Feature Request – Relationship Field

Solved

Feature Request – Relationship Field

  • As far as I can see from the documentation, the relationship field is only linked on one side. I think it should be possible by default or at least as an option, even in linked posts to create a relationship to the currently edited post.
    Because for a one-way relationship already exists the post object field, which does more or less the same, so I see no sense for the own field relationship.

  • If you are talking about bidirectional relationships there are plugins available for this. I do not know if the developers are planning to add native functionality to ACF, you would need to contact them to ask.

  • You cannot do it natively; as John mentioned, there is a plugin.

    Though ACF has its resource on how to do so with some coding know-how.
    https://www.advancedcustomfields.com/resources/bidirectional-relationships/

  • 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;
    }
  • Why is this not core ACF?

    The following is my opinion and experience from years of digging through ACF code and building code to extend ACF.

    The main issue for not including this in ACF, from my observation, is repeater and flexible content fields (both are repeaters really). Because of the way that ACF manages repeaters it is simply impossible to know match a specific row of a repeater in one post with a specific row of a repeater on another post. Especially considering that rows can be reordered on either side of the relationship.

    My assumption for why it was not included in ACF is that the developer did not want to provide the functionality if it could not be done under all conditions… because as soon as it was added there would be demands that it worked under all conditions.

    Could ACF add limits to not allow bidirectional fields in repeaters. Possibly, but currently ACF does not really allow different settings for field types dependent on if they are top level fields or sub fields. This could potentially be added. But again, I am assuming this has not been done because when it is done, as mentioned above, the developer would get many requests to make it work for sub fields.

    3rd Party plugins can limit how it can be used avoiding the need for the developer to add features that cannot be supported under all conditions. For example the plugin I created that does this only looks at top level fields and avoids the repeater problem by ignoring it completely. This is functionality that I simply refuse to add and consider “Out of Scope”.

    The fact that these bidirectional relationships can be coded only limits what they can do to the abilities of the coder. Might it be possible to do bidirectional fields for repeater sub fields? Maybe… for a specific use case, built and coded for that specific use case.

    What is the difference between Post Object and Relationship fields?

    There are 2 main differences.

    1. The UI presented to users
    2. The number of posts assumed as default.
      • Post Object fields assume a single post but can be made to allow selecting multiple. When allowing multiple selections no limits can be placed on the number of selections.
      • Relationship fields assume multiple selections but can be made to only allow a single selection or require and arbitrary min/max number of selections.

    I have always used these as 1) Post Object fields always allow only one selection. 2) Relationship fields always allow multiple selections.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.