acf-extended / ACF-Extended

🚀 All-in-one enhancement suite that improves WordPress & Advanced Custom Fields
https://www.acf-extended.com
239 stars 28 forks source link

Having issues with Bidectional relationship #85

Closed ThinkDevStudios closed 1 year ago

ThinkDevStudios commented 3 years ago

Describe the bug I created 3 post type mainly, subjects, documents and steps and tried connecting via bidirectional relationship but when i try to create a post object let say on steps.. The subject and documents does not update the relationship.

To Reproduce Subject uses a relationship field for documents Documents uses a relationship field for subjects Steps uses post object for both subject and documents

WordPress & ACF WordPress version: 5.8.1 ACF Pro version: 5.9.5 ACF extended: 0.8.8.6

ThinkDevStudios commented 3 years ago

Here is the exported Field Groups via PHP to recreate the issue

if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
    'key' => 'group_615c1fcf604d7',
    'title' => 'Document Fields',
    'fields' => array(
        array(
            'key' => 'field_615c1fd647eac',
            'label' => 'Steps',
            'name' => 'steps',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'steps',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => true,
                'acfe_bidirectional_related' => array(
                    0 => 'field_615bf826c20f3',
                ),
            ),
        ),
        array(
            'key' => 'field_615c270d7c879',
            'label' => 'Subject',
            'name' => 'subject',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'subjects',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => '1',
                'acfe_bidirectional_related' => array(
                    0 => 'field_615bf800c20f2',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'documents',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'left',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'acfe_display_title' => '',
    'acfe_autosync' => '',
    'acfe_form' => 0,
    'acfe_meta' => '',
    'acfe_note' => '',
));

acf_add_local_field_group(array(
    'key' => 'group_615bf7f0788d9',
    'title' => 'Steps Fields',
    'fields' => array(
        array(
            'key' => 'field_615bf800c20f2',
            'label' => 'Subject',
            'name' => 'subject',
            'type' => 'post_object',
            'instructions' => '',
            'required' => 1,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'subjects',
            ),
            'taxonomy' => '',
            'allow_null' => 0,
            'multiple' => 0,
            'return_format' => 'id',
            'save_custom' => 1,
            'save_post_type' => 'subjects',
            'save_post_status' => 'publish',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => true,
                'acfe_bidirectional_related' => array(
                    0 => 'field_615c270d7c879',
                ),
            ),
            'ui' => 1,
        ),
        array(
            'key' => 'field_615bf826c20f3',
            'label' => 'Document',
            'name' => 'document',
            'type' => 'post_object',
            'instructions' => '',
            'required' => 1,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'documents',
            ),
            'taxonomy' => '',
            'allow_null' => 0,
            'multiple' => 0,
            'return_format' => 'id',
            'save_custom' => 1,
            'save_post_type' => 'documents',
            'save_post_status' => 'publish',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => '1',
                'acfe_bidirectional_related' => array(
                    0 => 'field_615c1fd647eac',
                ),
            ),
            'ui' => 1,
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'steps',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'left',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'acfe_display_title' => '',
    'acfe_autosync' => '',
    'acfe_form' => 0,
    'acfe_meta' => '',
    'acfe_note' => '',
));

acf_add_local_field_group(array(
    'key' => 'group_615c2698d8d63',
    'title' => 'Subject Fields',
    'fields' => array(
        array(
            'key' => 'field_615c26a518adb',
            'label' => 'Documents',
            'name' => 'documents',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'documents',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => true,
                'acfe_bidirectional_related' => array(
                    0 => 'field_615c270d7c879',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'subjects',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'left',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'acfe_display_title' => '',
    'acfe_autosync' => '',
    'acfe_form' => 0,
    'acfe_meta' => '',
    'acfe_note' => '',
));

endif;
apsolut commented 3 years ago

I also notices issues, lets say Relationship is working but Taxonomy is not,... hm

acf-extended commented 3 years ago

Hello,

@ThinkDevStudios I just tested your code and it works, but you have some logic problems on your Steps post type.

The "Documents" bidirectional field on that Steps post type works fine, but you also have a "Subjects" field that is linked as bidirectional to an another "Subjects" field, but displayed on the Documents post type, that is configured to display "Subjects".

Note that on the "Steps" post type, if you display a bidirectional Post Object that will list Subjects, you have to display the counter part on the "Subjects" post type, otherwise the system won't work.

For example let's say you choose "Subject 2" on that "Step 1" post, then the bidirectional setting will search that "Subject 2" post and add the post id of the "Step 1" post (where the value has been selected by the user) in the related field. The bidirectional always has to have the counter part on the related post type/user/taxonomy you want to link.

As an example, here are the possible bidirectional setup:

Post <> Post
------------------------------
┌ Display a Post Object field on "Post Type A" showing "Post Type B"
â”” Display a Post Object field on "Post Type B" showing "Post Type A"

Taxonomy <> Taxonomy
------------------------------
┌ Display a Taxonomy field on "Taxonomy A" showing "Taxonomy B"
â”” Display a Taxonomy field on "Taxonomy B" showing "Taxonomy A"

Taxonomy <> User
------------------------------
┌ Display a Taxonomy field on "Users" showing "Taxonomy A"
â”” Display a User field on "Taxonomy A" showing "Users"

Taxonomy <> Post
------------------------------
┌ Display a Taxonomy field on "Post Type A" showing "Taxonomy A"
â”” Display a Post Object field on "Taxonomy A" showing "Post Type A"

User <> Post
------------------------------
┌ Display a User field on "Post Type A" showing "Users"
â”” Display a Post Object field on "Users" showing "Post Type A"

User <> User
------------------------------
┌ Display a User field on "Users" showing "Editor"
â”” Display a User field on "Users" showing "Admin"

In your case, here is what you did. Note how those two fields can simply not communicate between each others:

Post <> Post
------------------------------
â”” Display a Post Object field on "Post Type Steps" showing "Post Type Subjects"
┌ Display a Post Object field on "Post Type Documents" showing "Post Type Subjects"

@apsolut I checked the Taxonomy field as a bidirectional, and it also works fine. Please see the possible setup described above to make sure that correctly displayed the Taxonomy field on a Post/User, but also setup the correct counter-part (Post Object/User field) on the said taxonomy.

Hope it helps!

Have a nice day!

Regards.

ThinkDevStudios commented 3 years ago

Hi! I am really having a hard time figuring this out. still does not work. I have 2 post types mainly subjects, documents and steps Subject can have multiple documents and steps is connected to both documents and subject

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_615c1fcf604d7',
    'title' => 'Document Fields',
    'fields' => array(
        array(
            'key' => 'field_615c1fd647eac',
            'label' => 'Steps',
            'name' => 'steps',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'steps',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => true,
                'acfe_bidirectional_related' => array(
                    0 => 'field_615bf826c20f3',
                ),
            ),
        ),
        array(
            'key' => 'field_615c270d7c879',
            'label' => 'Subject',
            'name' => 'subject',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'subjects',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => '1',
                'acfe_bidirectional_related' => array(
                    0 => 'field_615c26a518adb',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'documents',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'left',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'acfe_display_title' => '',
    'acfe_autosync' => '',
    'acfe_form' => 0,
    'acfe_meta' => '',
    'acfe_note' => '',
));

acf_add_local_field_group(array(
    'key' => 'group_615bf7f0788d9',
    'title' => 'Steps Fields',
    'fields' => array(
        array(
            'key' => 'field_615bf800c20f2',
            'label' => 'Subject',
            'name' => 'subject',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 1,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'subjects',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => '1',
                'acfe_bidirectional_related' => array(
                    0 => 'field_618b46a59971c',
                ),
            ),
        ),
        array(
            'key' => 'field_615bf826c20f3',
            'label' => 'Document',
            'name' => 'document',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 1,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'documents',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => '1',
                'acfe_bidirectional_related' => array(
                    0 => 'field_615c1fd647eac',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'steps',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'left',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'acfe_display_title' => '',
    'acfe_autosync' => '',
    'acfe_form' => 0,
    'acfe_meta' => '',
    'acfe_note' => '',
));

acf_add_local_field_group(array(
    'key' => 'group_615c2698d8d63',
    'title' => 'Subject Fields',
    'fields' => array(
        array(
            'key' => 'field_615c26a518adb',
            'label' => 'Documents',
            'name' => 'documents',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'documents',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => true,
                'acfe_bidirectional_related' => array(
                    0 => 'field_615c270d7c879',
                ),
            ),
        ),
        array(
            'key' => 'field_618b46a59971c',
            'label' => 'Steps',
            'name' => 'steps',
            'type' => 'relationship',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'post_type' => array(
                0 => 'steps',
            ),
            'taxonomy' => '',
            'filters' => array(
                0 => 'search',
            ),
            'elements' => '',
            'min' => '',
            'max' => '',
            'return_format' => 'id',
            'acfe_bidirectional' => array(
                'acfe_bidirectional_enabled' => true,
                'acfe_bidirectional_related' => array(
                    0 => 'field_615bf800c20f2',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'subjects',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'left',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'acfe_display_title' => '',
    'acfe_autosync' => '',
    'acfe_form' => 0,
    'acfe_meta' => '',
    'acfe_note' => '',
));

endif;

acf-extended commented 3 years ago

Hello,

Please read my answer above. It looks like you still have the same problem as above.

If you're not sure about how it works, please setup a simple bidirection between 2 post types with 2 simple field groups that are configured to display each other using the Admin UI (and not PHP code).

You can follow the instructions on the documentation:

  1. Create a field group “Page: Relation” displayed on the post type: page
  2. Inside it, create a relationship field, allowing the post type: post
  3. Create a field group “Post: Relation” displayed on the post type: post
  4. Inside it, create a relationship field, allowing the post type: page
  5. Activate the “Bidirectional” setting and select “Page: Relation” relationship field
  6. Edit any page, and select any post in the relationship field
  7. The page is now also saved in the said post relationship field

Then once this simple setup works, add other fields in your field groups that have other bidirectional settings.

Hope it helps!

Regards.