Syncing Group Taxonomy Advanced Field with a Taxonomy Field

Support MB Group Syncing Group Taxonomy Advanced Field with a Taxonomy FieldResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #40357
    NorCalNorCal
    Participant

    Hello, 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,

    #40368
    PeterPeter
    Moderator

    Hello,

    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/

    #41211
    Timo WesselsTimo Wessels
    Participant

    Hi Peter is there any real-world example for that?

    Like
    taxonomy name = Drink Recipe
    taxonomy-slug = drink-recipe

    term name = Espresso Martini
    term = espresso martini

    because I checked the https://docs.metabox.io/actions/rwmb-after-save-field/ but just more "documentation"

    #41246
    PeterPeter
    Moderator

    Hello,

    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/

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.