Hey John,
Thanks for the response. I actually just discovered that what I wanted to do can actually be done, with a Post Object and allow Multiple Values.
This is then essentially a relationship in the format of a taxonomy field.
Hey again,
Hoping this will eventually help someone else, so I am making an update as I have continued working on this.
While working on this, I have failed to find a way to run this using the update_value hook, and therefore currently can only be done when the post is saved. Also just to point out, the posted code is only one side of the script, to do it bidirectional, duplicate from determining the post type and reverse all the field name. I also realized it wouldn’t delete rows if they were removed. So…..
First we have our action for the save post function with a value great then 10 so it runs after the values have saved
add_action('acf/save_post', 'save_post_functions', 20);
Then our function
function save_post_functions( $post_id )
//get the post type and do an if to determine if its a person or department
if ( get_post_type( ) == ‘people’) {
//get the persons departments and run through them
$people_departments = get_field(‘people_departments’);
if ($people_departments) {
foreach($people_departments as $pdepartments) {
//make sure the select in the row has a value, if not we do nothing
if ($pdepartments['select']!='') {
//get the departments people
$department_people = get_field('department_people',$pdepartment['select']);
//see if the person exists in the department already
$key = array_search($post_id, array_column($department_people, 'select'));
//build the row to update/add
$new_row = array( 'select' => $post_id, 'title' => $pdepartments['title'], 'id' => $pdepartments['id']);
//if $key equals false we add the row, otherwise update it (with key+1 since ACF rows start at 1
if ($key === false) {
add_row('department_people',$new_row,$pdepartment['select']);
} else {
update_row('department_people',$key+1,$new_row,$pdepartment['select']);
}
}
//that parts done
//now we check to see what needs to be deleted.
//first we get all departments with this persons post_ID using the custom post_ids_from_meta function (below)
$departments_with_person = post_ids_from_meta('department_people_%_select', $post_id);
//go through each department and see if it still exists for this person
foreach($departments_with_person as $dwp) {
list($dwp_id,$row) = explode("|",$dwp);
$key2 = array_search($dwp_id, array_column($people_departments, 'select'));
//if the department does not exists on the person, delete the person from the department
if($key2 === false) {
delete_row('department_people',$row+1,$dwp_id);
}
}
}
}
}
//custom function to get all the departments for this person (or all the people for the department)
`function post_ids_from_meta($key, $value) {
global $wpdb;
if(!is_array($post_ids)) { $post_ids = array(); }
$metas = $wpdb->get_results(“SELECT post_id,meta_key FROM wp_postmeta WHERE meta_value = ‘”.$value.”‘ AND meta_key LIKE ‘”.$key.”%'”);
if($metas) {
foreach($metas as $meta) {
$row_number = explode(‘_’,$meta->meta_key);
array_push($post_ids,$meta->post_id.”|”.$row_number[2]);
}
}
return($post_ids);
}
Hey John,
No worries. I posted and then solved it pretty quickly. Didn’t really give you a chance to see it.
Yes, that is the example I was talking about. The above post I made is working great for my purposes, and I have added additional if($field_name statements to match up all my desired bidirectional relationships to eachother.
I understand the idea if it isn’t already set, but so far all is working well. Fingers Crossed.
Thanks!
Just wanted to post that I figured out my errors on this, and now have it working!
First in the filter it should have been
add_filter('acf/update_value/type=relationships', 'bidirectional_acf_update_value', 10, 3);
not
add_filter('acf/update_value/name=related_posts', 'bidirectional_acf_update_value', 10, 3);
referencing the relationship field type
The other issue was when assigning the second field_name and field_key I had a single =
if($field_name = 'artist_releases') { $field_name2 = 'release_artists'; $field_key2 = 'field_5bf7a2e215e5c'; }
if($field_name = 'release_artists') { $field_name2 = 'artist_releases'; $field_key2 = 'field_5bf79ed45b371'; }
should have been
if($field_name == 'artist_releases') { $field_name2 = 'release_artists'; $field_key2 = 'field_5bf7a2e215e5c'; }
if($field_name == 'release_artists') { $field_name2 = 'artist_releases'; $field_key2 = 'field_5bf79ed45b371'; }
This now works for bidirectional relationships with different names.
Thanks!
Just wanted to add since I’ve still been attempting to solve this.
I was able to get the update_sub_rows to work on it’s own outside of the rest of my script.
It seems to stop working though once I put an IF statement before it.
I had a similar issue recently also working on show listings.
You can do a regular query retrieving the posts then do a get_fields for all fields of that post, then sort the array. Check out Answer 1 below for that one.
https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value
I chose to create a custom query joining to the postmeta table twice. Once for the date and once for the time. Like
SELECT * WPPREFIX_posts p
INNER JOIN WPPREFIX_postmeta m1 ON p.ID = m1.post_id
INNER JOIN WPPREFIX_postmeta m2 ON p.ID = m2.post_id
WHERE m1.meta_key = 'start_date' AND m2.meta_key = 'start_time'
ORDER BY m1.meta_value,m2.meta_value
This allows you to sort by each meta value.
Hope this helps
Hello,
I’m fairly new here, but can say that bumping your post is probably not the best way to get an answer. Just wait, if someone is able to help, they will.
I will try to help.
Do you have the Post_Object Return Format set to return the PostID or the Post Object?
Right after your get_field, try printing out the $post_objects array and see if there is data in the returned array, and please provide the results that are displayed on the page.
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!
🗓️ Just one more day until the next session of ACF Chat Fridays! Make sure to register for the latest updates on what’s coming in ACF 6.1!
— Advanced Custom Fields (@wp_acf) March 30, 2023
📆 Friday 31st March 3pm UTC
👉 Register here - https://t.co/3UtvQbDwNm pic.twitter.com/7xtEJakeQN
© 2023 Advanced Custom Fields.
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.