acf-composer
acf-composer copied to clipboard
How to create a bidirectional custom field?
In the current version of ACF Pro you are able to create a custom field with a bidirectional relationship. Is this possible with acf-composer? I do not see any related information in this repository or the cheatsheet.
I'm pretty sure all config options will be passed through to ACF, so it should 'just work' :) give it a try! + PR's always welcome if improvements needed in the docs
This should help from the ACF Builder repo - https://github.com/StoutLogic/acf-builder/issues/121#issuecomment-1131587567
Currently, the Bidirectional setting is only available for the Relationship, User, Taxonomy, and Post Object fields
Source: ACF docs
Thanks for the quick response, looks promising. However, on my end it does not work. Here is my code:
<?php
...
class ProductLocation extends Field
{
/**
* The field group.
*/
public function fields(): array
{
$productLocation = Builder::make('product_location');
$productLocation
->setLocation('post_type', '==', 'product-location')
->addRelationship('location_product_groups', [
'post_type' => 'product-group',
'instructions' => '',
'acfe_bidirectional' => [
'acfe_bidirectional_enabled' => true,
'acfe_bidirectional_related' => [
'field_group_product_locations',
],
],
'filters' => [
0 => 'search',
],
]);
return $productLocation->build();
}
}
<?php
...
class ProductGroup extends Field
{
/**
* The field group.
*/
public function fields(): array
{
$productGroup = Builder::make('product_group');
$productGroup
->setLocation('post_type', '==', 'product-group')
->addRelationship('group_product_locations', [
'post_type' => 'product-location',
'instructions' => '',
'acfe_bidirectional' => [
'acfe_bidirectional_enabled' => true,
'acfe_bidirectional_related' => [
'field_location_product_groups',
],
],
'filters' => [
0 => 'search',
],
]);
return $productGroup->build();
}
}
As you can see I use the value in the array key acfe_bidirectional_related with the name I set in the other fields instance. I also tried the name of what the page source tells me in the dashboard. Over there the name is field_product_location_location_product_groups and field_product_group_group_product_locations. But when I try this it also does not work.
Did you ever manage to figure this out? Can you try the latest version of ACF Composer?
Sorry for the late response. I missed your reply.
Sadly, I did not figure out what is going wrong. To this day I still have some projects where we'd like to apply bi-directional fields, but it's not working. I am running the latest version of ACF Composer.
@tislars 'acfe_bidirectional' is using ACF Extended, which is no longer needed for bidirectional relationship since it's already included in ACF natively. Try with this:
$productGroup
->setLocation('post_type', '==', 'product-group')
->addRelationship('group_product_locations', [
'post_type' => 'product-location',
'instructions' => '',
'bidirectional' => true,
'bidirectional_target' => [
0 => 'related_field_name_here'
],
'filters' => [
0 => 'search',
],
]);
I ran into the same issue, but managed to make it work. Here's how I set up the fields:
$fields = Builder::make('actor_fields');
$fields
->setLocation('post_type', '==', 'actor')
->addPostObject('videos', [
'name' => 'videos',
'post_type' => ['video'],
'return_format' => 'id',
'multiple' => true,
'ui' => true,
'bidirectional' => 1,
'bidirectional_target' => ['field_video_fields_actors'],
'allow_null' => false,
]);
$fields = Builder::make('video_fields');
$fields
->setLocation('post_type', '==', 'video')
->addPostObject('actors', [
'name' => 'actors',
'post_type' => ['actor'],
'return_format' => 'id',
'multiple' => true,
'ui' => true,
'bidirectional' => 1,
'bidirectional_target' => ['field_actor_fields_videos'],
'allow_null' => false,
]);