Support

Account

Home Forums ACF PRO Bidirectional Relationships require both posts to be manually updated

Solved

Bidirectional Relationships require both posts to be manually updated

  • I’ve had this problem for a while, on multiple sites, and am wondering if anyone has a solution. I use the bidirectional relationships snippet to relate posts in two different custom post types. It *almost* works perfectly except the relationship doesn’t show up on the frontend for the second post until it too is updated. Instead I get this PHP notice:

    Notice
    : Undefined offset: 0 in
    XXXXXXXXXXXXXXX\wp-includes\class-wp-query.php
    on line
    3149

    If I do a print_r on the field, I get an empty array. As soon as I update the second post, that goes away, the relationship shows up fine on the frontend, and print_r returns an array of the post ids.

    Is there any fix for this so that I don’t have to resave both posts? That kind of defeats the purpose of the otherwise awesome bidirectional relationships snippet.

  • If you are updating the value on the other end where a value was not previously stored by ACF and you are using the field name when calling update_value() then this will cause the problem you are seeing. When you use update_field() on a field that has never been saved before then you must use the field key instead of the field name. This in explained in the documentation https://www.advancedcustomfields.com/resources/update_field/

  • I thought it might be something like that. Since I have two different fields (same field names but different keys) I’ll have to use something like this, yes? https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-reciprocal-relationship.php

    But if the field is has the same key on both sides, I can use the the normal snipped (https://www.advancedcustomfields.com/resources/bidirectional-relationships/) but just change acf/update_value/name=related_posts to acf/update_value/key=field_cba9876543210 ?

  • That code is mine.

    Also this is mine and works where the field names are the same and it deals with the keys. https://wordpress.org/plugins/post-2-post-for-acf/. The reason that it works is that it basically reads thought all of the ACF fields to find all the fields with the same name and then uses the key…. well, it actually uses update_post_meta() and bypasses the acf function completely. It also has some other features like checking the max number of posts that the field on the other side of the relationship can have so it won’t have too many.

    The basic code will work where you use the same field, basically a field group with one field for the relationship where you have the group on multiple post types. It’s really designed for the same field in the same field group.

  • Thanks! I’m going to give your plugin a try.

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

The topic ‘Bidirectional Relationships require both posts to be manually updated’ is closed to new replies.