Hi @marc_a,
Thank you again for your guidance. I have got around the issue in a similar, but slightly different way.
As you know my CPTs are Memorial and Casualty. Memorials has a Relationship ACF field = memorial_casualties. Casualties has a Relationship ACF field = casualty_memorial_listed.
I then have two sections of code for my functions.php
:
function link_casualty_to_memorial( $value, $post_id, $field ) {
$field_name = $field['name'];
$global_name = 'is_updating_' . $field_name;
if ( ! empty( $GLOBALS[ $global_name ] ) ) {
return $value;
}
$GLOBALS[ $global_name ] = 1;
if ( is_array( $value ) ) {
foreach ( $value as $memorial_post_id ) {
$casualties = get_field( 'field_63daac69ede1b', $memorial_post_id );
if ( empty( $casualties ) ) {
$casualties = array();
}
if ( in_array( $post_id, $casualties ) ) {
continue;
}
$casualties[] = $post_id;
update_field( 'field_63daac69ede1b', $casualties, $memorial_post_id );
}
}
$old_value = get_field( $field_name, $post_id, false );
if ( is_array( $old_value ) ) {
foreach ( $old_value as $memorial_post_id ) {
if ( is_array( $value ) && in_array( $memorial_post_id, $value ) ) {
continue;
}
$casualties = get_field( 'field_63daac69ede1b', $memorial_post_id, false );
if ( empty( $casualties ) ) {
continue;
}
$pos = array_search( $post_id, $casualties );
unset( $casualties[ $pos ] );
update_field( 'field_63daac69ede1b', $casualties, $memorial_post_id );
}
}
$GLOBALS[ $global_name ] = 0;
return $value;
}
function link_memorial_to_casualty( $value, $post_id, $field ) {
$field_name = $field['name'];
$global_name = 'is_updating_' . $field_name;
if ( ! empty( $GLOBALS[ $global_name ] ) ) {
return $value;
}
$GLOBALS[ $global_name ] = 1;
if ( is_array( $value ) ) {
foreach ( $value as $casualty_post_id ) {
$memorials = get_field( 'field_63da97eec7d7f', $casualty_post_id );
if ( empty( $memorials ) ) {
$memorials = array();
}
if ( in_array( $post_id, $memorials ) ) {
continue;
}
$memorials[] = $post_id;
update_field( 'field_63da97eec7d7f', $memorials, $casualty_post_id );
}
}
$old_value = get_field( $field_name, $post_id, false );
if ( is_array( $old_value ) ) {
foreach ( $old_value as $casualty_post_id ) {
if ( is_array( $value ) && in_array( $casualty_post_id, $value ) ) {
continue;
}
$memorials = get_field( 'field_63da97eec7d7f', $casualty_post_id, false );
if ( empty( $memorials ) ) {
continue;
}
$pos = array_search( $post_id, $memorials );
unset( $memorials[ $pos ] );
update_field( 'field_63da97eec7d7f', $memorials, $casualty_post_id );
}
}
$GLOBALS[ $global_name ] = 0;
return $value;
}
add_action( 'acf/update_value/name=casualty_memorial_casualties', 'link_casualty_to_memorial', 10, 3 );
add_action( 'acf/update_value/name=memorial_casualties', 'link_memorial_to_casualty', 10, 3 );
In the Relationship fields themselves, the “Filter By Post Type” field references the post_type that is being linked to.
This works for me and does what I need. I hope it is useful for other people looking to deal with Relationships between post_types.
Thanks again,
Christopher