Support

Account

Home Forums General Issues Bidirectional between Post and User

Solved

Bidirectional between Post and User

  • I have the need to link users to a certain post type as well as the post type back to the users. I would like to do something similar, to what is shown here (https://support.advancedcustomfields.com/forums/topic/bidirectional-relationships-and-multiple-post-types/) but in my case I am using the User Relationship field in my CPT and am using the Post Relationship field in my User.

    Has anyone done this before, or have any insights in how to accomplish something like this. I’m guessing the reason the tutorial and the function in the thread above doeesn’t work is that the User and Post Relationship field store their data a different way. I can hack it up and most likely get it to work, just checking to see if something else is out there first.

  • They actually store information the same way. The difference is that relationship fields store an array of post IDs and the user field stores an array of user IDs. The only other difference is where you’re looking at the values, using the post ID or "user_{$user_id}"

  • So If i use the example answer from that thread, and call the functions with two filters with the correct field keys it should work without modification?

  • So I have made the modifications to the file listed in the other thread, you can view it here https://gist.github.com/codytooker/56dd0bc7a6893d5e7bf5a5c25bc5e8ea

    It probably needs to be clean up a bit with better comments but it works for what I need.

    Basically I had to test which field was coming from a user and if so have to remove “user_” from the ID, later on in the file I have to use get_user_meta and update_user_meta in places instead of post_meta, I do this with a flag to easily switch between both.

    • Boogey

    • June 14, 2019 at 2:52 am

    I had the same need as the OP, for a current project.

    mynameiscody07: Can your github code be used as a drop-in replacement for the original ACF bidirectional tutorial code?

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.