AdvancedCustomFields/acf

The save_post_terms array values assigned but not saved for bidirectional values

Opened this issue · 0 comments

Cannot save post terms submitted by taxonomy edit form with relationship bidirectional field.

To Reproduce

Create custom post type People and custom taxonomy Role for post type People.

Add taxonomy field roles to People and enable save and load terms options.

image

Add relationship field people_order for Role taxonomy.

Enable bidirectional relations between roles and people_order fields.

Create new People post and add a single value for roles field.
image

Edit role term by removing value from people_order field.
image
image

Expected behavior
single-people.php

<p>Debug roles get_field: <?= json_encode(get_field('roles')) ?></p>
<p>Debug roles get_the_terms: <?= json_encode(get_the_terms(get_the_ID(), 'role')) ?></p>
Debug roles get_field: false
Debug roles get_the_terms: false

Actual behavior — the value still exists (loaded from wp object terms?)

Debug roles get_field: [55]
Debug roles get_the_terms: [{"term_id":55,"name":"Director","slug":"director","term_group":0,"term_taxonomy_id":55,"taxonomy":"role","description":"","parent":0,"count":1,"filter":"raw"}]

image

Code
acf-export.json

Version Information:

  • WordPress Version 6.4.2
  • PHP Version 8.1.2
  • ACF Version ACF PRO 6.2.4
  • Browser Chrome 120

Additional context

The problem looks like solved when I comment this lines:

class-acf-field-taxonomy.php:381

// append
$this->save_post_terms[ $taxonomy ] = array_merge( $old_term_ids, $term_ids );

// if called directly from frontend update_field().
-if ( ! did_action( 'acf/save_post' ) ) {
    $this->save_post( $post_id );
-    return $value;
-}

return $value;

Looks like $this->save_post call is needed for this case.