Syncing Group Taxonomy Advanced Field with a Taxonomy Field
- This topic has 3 replies, 3 voices, and was last updated 2 years, 1 month ago by
Peter.
-
AuthorPosts
-
January 31, 2023 at 12:53 PM #40357
NorCal
ParticipantHello, I need a taxonomy field within a Group to set the post terms.
My understanding is that taxonomy fields within a Group will not work to set the post terms, is there any other way to set the post terms within a Group? Maybe by creating a Taxonomy Advanced field within the group that will somehow sync with a separate Taxonomy field outside the group to set the post terms?
Thank you,
January 31, 2023 at 10:59 PM #40368Peter
ModeratorHello,
You are correct. It is possible to set the post terms based on the taxonomy_advanced field value in a group. If you are familiar with coding you can create some lines of code to set the post terms. Please read more on the documentation
https://docs.metabox.io/actions/rwmb-after-save-field/
https://developer.wordpress.org/reference/functions/wp_set_object_terms/March 25, 2023 at 8:37 PM #41211Timo Wessels
ParticipantHi Peter is there any real-world example for that?
Like
taxonomy name = Drink Recipe
taxonomy-slug = drink-recipeterm name = Espresso Martini
term = espresso martinibecause I checked the https://docs.metabox.io/actions/rwmb-after-save-field/ but just more "documentation"
March 28, 2023 at 7:38 PM #41246Peter
ModeratorHello,
For example, I have a taxonomy field and a taxonomy_advanced subfield in a group field, like this
$meta_boxes[] = [ ... 'fields' => [ [ 'name' => __( 'Taxonomy', 'your-text-domain' ), 'id' => 'taxonomy_field', 'type' => 'taxonomy', 'taxonomy' => ['drink-recipe'], 'field_type' => 'select_advanced', ], [ 'name' => __( 'Group', 'your-text-domain' ), 'id' => 'group_field', 'type' => 'group', 'fields' => [ [ 'name' => __( 'Taxonomy Advanced', 'your-text-domain' ), 'id' => 'taxonomy_advanced_field', 'type' => 'taxonomy_advanced', 'taxonomy' => ['drink-recipe'], 'field_type' => 'select_advanced', ], ], ], ], ];
Then the code to set the post term from the taxonomy_advanced field would be:
add_action( 'rwmb_group_field_after_save_field', function ( $null, $field, $new, $old, $object_id ) { $term_id = (int)$new['taxonomy_advanced_field']; wp_set_object_terms( $object_id, $term_id, 'drink-recipe' ); }, 10, 5 );
This is the example code, if it does not work on your site, please contact us with the link below and request a customization service, our development team will help you to check the issue.
https://metabox.io/contact/ -
AuthorPosts
- You must be logged in to reply to this topic.