• Resolved vectrolux

    (@vectrolux)


    I’ve got several post types on a site I am working on and am having trouble with the two way relationships I have created. For example. There are Locations, Conditions, and Treatments post types. I’ve got a two way relationship with Locations and Conditions and Treatments using ACF Post-2-Post. If we delete a Condition or Treatment it is not removing the relationship from Locations. Is this normal? If not, what might be causing this? If we change the title of a Treatment it does not seem to be updating in the Location post either.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author John Huebner

    (@hube2)

    This is normal behavior. There is not check done when a post is deleted to remove any relationship from other fields.

    This is the same way that ACF itself works. If you were not using a bidirectional relationship, if you edited post A to create a relationship with post B and then delete post B the relationship would still exist in post A. However, when you get a field ACF will not return posts that have been deleted, or that are in the trash.

    Also, when you update the other post ACF should see that the post no longer exists and remove it from the relationship, though I’m not sure if this is the case when in the trash.

    If you are getting the value without formatting it get_field('relationship', false, false) you will get the deleted post ID, under this condition it would be up to you to check to make sure each post exists.

    Thread Starter vectrolux

    (@vectrolux)

    Thank you for your reply. I will look into modifying my queries to to check post status.

    The other issue I am having is that the relationships do not seem to be showing up in the both posts until they are opened and updated. Is this normal? What I mean is I open post A and add a relationship to post B. It does not show up in post B until I open it and update the post. Shouldn’t this relationship appear in both without having to update both posts?

    Plugin Author John Huebner

    (@hube2)

    I don’t know why you are having the second issue, but it’s not how it should be working. The only think that comes to mind is a caching issue. If you are running any type of a caching plugin the update of the custom field will not cause the cache for the post on the other side of the relationship to be cleared. The post is not updated, only the field in question. Saving the post will generally clear the cache for that page.

    Thread Starter vectrolux

    (@vectrolux)

    Thanks again. Should it cause problems that I have a relationship field for both Conditions and Treatments in the Locations post type? I also have Locations as a related post type in both the Conditions and the Treatments post types. The field is the same name in each just not the same available post types.

    In a somewhat related issue we are using the plugin FacetWP and it is not working as we hoped because the two way relationships are not being updated when saved. The developers of that plugin asked about hooks for FacetWP to latch onto.

    Ideally there’d be hooks that fire when a post has been modified, within the add_relationship() and remove_relationship() methods, e.g.

    add_action( ‘acf/post2post/relationship_updated’, $post_id ); // fires when a referenced (bi-directional) post has been updated

    Plugin Author John Huebner

    (@hube2)

    So, let me understand. The issue with them not appearing on the front of the site is due to them not appearing in FacetWP? because FacetWP does not get an indication that the index for the post needs to be update? or something along those lines?

    Thread Starter vectrolux

    (@vectrolux)

    I’m not sure what is causing the issue. FacetWP allows us to filter down a query of posts based on taxonomies and ACF fields. It’s limited to a specific query per page. I’m not sure how it would prevent the posts from showing in queries outside the one with the CSS class it’s linked to.

    The issue I am having is I want to filter results from these post2post relationship fields and they are not updating on the backend unless the related post is opened and re-saved. I thought maybe it had to do with the way I am using your plugin.

    Plugin Author John Huebner

    (@hube2)

    I have used facetWP in the past. It saves an index of the posts and related fields. It is not updating the index. You either need to go into the facetwp admin and run the indexing manually, or you can trigger the re-indexing by calling a function in facetwp. There is currently no action triggered in this plugin when the post is updated on the other end.

    I can look at adding an action after the update is made that you can use to trigger the other plugin to do its thing.

    Hey John, FacetWP author here — happy to answer any further questions.

    Long story short, FacetWP needs to know when each of the related posts has been changed. Here’s the code in question: https://github.com/Hube2/acf-post2post/blob/master/acf-post2post.php#L71-L82

    The above code loops through each related post and saves the bi-directional data via update_post_meta calls. I could technically use the updated_postmeta hook to listen for changes, but it’s super inefficient. Ideally (hopefully?) we could get a hook added that fires when a related post has been changed by ACF-Post2Post.

    Plugin Author John Huebner

    (@hube2)

    If I add a hook, I would like it to be a generic one that could be used by anyone. There was a suggestion above to use something like
    add_action('acf/post2post/relationship_updated', $post_id);
    or from my point of view
    do_action('acf/post2post/relationship_updated', $post_id);
    My only question would be, is this something that should be fired on every post update or would it be more efficient and is it possible to fire it once with an array of post IDs that have been updated?

    Plugin Author John Huebner

    (@hube2)

    Just pushed a new version, I have added 2 action hooks

    acf/post2post/relationship_updated is fired after each post is updated and passes the post ID of the post that was updated.

    
    add_action('acf/post2post/relationship_updated', 'my_post_updated_action');
    function my_post_updated_action($post_id) {
      // $post_id == the post ID that was updated
      // do something after the related post is updated
    }
    

    alternately you can use acf/post2post/relationships_updated please note the subtle difference. This is fired after all updates are made and passes an array containing all the post IDs that were updated.

    
    add_action('acf/post2post/relationships_updated', 'my_post_updated_action');
    function my_post_updated_action($posts) {
      // $posts == and array of post IDs that were updated
      // do something to all posts after update
      foreach ($posts as $post_id)
        // do something to post
      }
    }
    
    • This reply was modified 5 years, 9 months ago by John Huebner.

    @hube2 Sorry for the late reply, didn’t realize I wasn’t auto-subscribed to this thread. Anywho, the hooks look great, thanks for adding them!

    
    // re-index bidirectional posts (ACF-Post2Post)
    add_action( 'acf/post2post/relationship_updated', function( $post_id ) {
        if ( function_exists( 'FWP' ) ) {
            FWP()->indexer->index( $post_id );
        }
    });
    
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Deleting and Changing Post Titles’ is closed to new replies.