Home › Forums › ACF PRO › Getting number of times selected from relationship field › Reply To: Getting number of times selected from relationship field
I didn’t think about that until you said it. What you really need is to have this count stored somewhere else. The idea you have is right. You need to create another custom field, and it does not even need to be part of ACF since you’ll only reference it in code.
You will need to update this other field whenever the relationship field is updated and you’ll not only need to know what posts are selected you’re also going to need to know if a relationship is removed.
You can use the acf/update_value filter for this. I’m not going to go into all the code but I will give you an example. The ACF 2 way relationship plugin I created does something similar. When it relationship field is updated it compares that old values with the new values and updates other posts as needed. https://github.com/Hube2/acf-post2post/blob/master/acf-post2post.php
What you need to to is get the old values, compare them and then update a post meta field in each of the related posts with the correct count.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.