From b828d52ff3b85534c09b5c9e9ce52c9e89606328 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sun, 14 Jul 2024 19:51:35 -0500 Subject: [PATCH] Add support to Sync an associated WP taxonomy with a relationship field --- classes/fields/pick.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/classes/fields/pick.php b/classes/fields/pick.php index 7b25a918d5..e16bb65171 100644 --- a/classes/fields/pick.php +++ b/classes/fields/pick.php @@ -441,6 +441,15 @@ public function options() { 'default' => '', 'type' => 'text', ], + static::$type . '_sync_taxonomy' => [ + 'label' => __( 'Sync associated taxonomy with this relationship', 'pods' ), + 'help' => __( 'This will automatically sync the associated taxonomy terms with the value of this relationship if this field is on a Pod that is a Post Type. If the associated taxonomy terms are different, they will be overridden on save.', 'pods' ), + 'wildcard-on' => [ + static::$type . '_object' => [ + '^taxonomy-.*$', + ], + ], + ] ]; $post_type_pick_objects = array(); @@ -2015,6 +2024,22 @@ public function save( $value, $id = null, $name = null, $options = null, $fields self::$api->delete_relationships( $remove_ids, $id, $related_pod, $related_field ); } + // Handle syncing of taxonomy terms. + if ( ! empty( $pod['type'] ) && 'post_type' === $pod['type'] && ! empty( $options[ static::$type . '_sync_taxonomy' ] ) ) { + // Check if post type has the same attached taxonomy. + $taxonomies_available = get_object_taxonomies( $pod['name'] ); + $taxonomies_available = array_flip( $taxonomies_available ); + + if ( $options instanceof Field || $options instanceof Value_Field ) { + $taxonomy_name = $options->get_related_object_name(); + + if ( isset( $taxonomies_available[ $taxonomy_name ] ) ) { + // Update the taxonomy terms for the current post. + wp_set_post_terms( $id, $value_ids, $taxonomy_name ); + } + } + } + if ( ! $no_conflict ) { pods_no_conflict_off( $related_pod['type'] ); }