diff --git a/php/datasource/class-fieldmanager-datasource-post.php b/php/datasource/class-fieldmanager-datasource-post.php index 0a343cbe19..ad6f1fe951 100644 --- a/php/datasource/class-fieldmanager-datasource-post.php +++ b/php/datasource/class-fieldmanager-datasource-post.php @@ -236,12 +236,16 @@ public function presave_alter_values( Fieldmanager_Field $field, $values, $curre * @return string */ public function presave( Fieldmanager_Field $field, $value, $current_value ) { - if ( empty( $value ) ) { + if ( $this->only_save_to_post_parent ) { + $current_value = $this->preload_alter_values( $field, $current_value ); + } + + if ( empty( $value ) && empty( $current_value ) ) { return; } $value = intval( $value ); - if ( ! empty( $this->publish_with_parent ) || ! empty( $this->reciprocal ) ) { + if ( ! empty( $value ) && ( ! empty( $this->publish_with_parent ) || ! empty( $this->reciprocal ) ) ) { // There are no permissions in cron, but no changes are coming from a user either. if ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) { $post_type_obj = get_post_type_object( get_post_type( $value ) ); diff --git a/tests/php/test-fieldmanager-datasource-post.php b/tests/php/test-fieldmanager-datasource-post.php index 1ea406f1af..5759e40c38 100644 --- a/tests/php/test-fieldmanager-datasource-post.php +++ b/tests/php/test-fieldmanager-datasource-post.php @@ -368,6 +368,7 @@ public function test_post_parent_nested() { $html ); } + /** * Test save_to_post_parent_only logic */ @@ -556,4 +557,31 @@ public function test_inherited_repeatable_post_parent_invalid() { ) ); } + + /** + * Test save_to_post_parent_only logic + */ + public function test_unsetting_post_parent_only() { + $fm = new Fieldmanager_Autocomplete( + array( + 'name' => 'test_parent', + 'datasource' => new Fieldmanager_Datasource_Post( + array( + 'only_save_to_post_parent' => true, + 'query_args' => array( + 'post_type' => 'post', + ), + ) + ), + ) + ); + + $this->save_values( $fm, $this->child_post_a, $this->parent_post->ID ); + $child = get_post( $this->child_post_a->ID ); + $this->assertEquals( $this->parent_post->ID, $child->post_parent ); + + $this->save_values( $fm, $this->child_post_a, '' ); + $child = get_post( $this->child_post_a->ID ); + $this->assertEquals( null, $child->post_parent ); + } }