Skip to content

Commit

Permalink
Add support to Sync an associated WP taxonomy with a relationship field
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Jul 15, 2024
1 parent f9d6f85 commit b828d52
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions classes/fields/pick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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'] );
}
Expand Down

0 comments on commit b828d52

Please sign in to comment.