Update Taxonomy from Values Stored in Relationship Field

[ad_1]

Hi, I am studying the documentation of this plugin to see if it can do for me and I must say that the plugin for sure is what I was looking for, but to use it I will need to study more and maybe take advantage of your help.

I’ve created a custom post type “Ricette” and a custom taxonomy “Ingredienti” and I added the taxonomy to the post type “Ricette” using a relationship field, but I figured out that there is no actual relationship between the taxonomy and the recipe:

If I add a new recipe and I select an ingredient from the taxonomy “Ingredienti”, for example garlic, after saving the recipe there is no correlation between the recipe and the taxonomy: If i clic on the taxonomy garlic I don’t see the recipe just made.

After some digging I found this code snippet, but I need some help:
https://docs.pods.io/code-snippets/update-taxonomy-value-stored-relationship-field/

In the example the taxonomy field is pods_genre and is related to the taxonomy genre,
in may case the taxonomy filed is ingredients_relation_field and the taxonomy is ingrediente, so I tried to modify the code like that:

/**
 * Update Taxonomy With Value Of Field Related To A Taxonomy
 * Takes the value of a single- or multi-select field 'pods_genre' related to the taxonomy 'genre' and updates the taxonomy with the term set in the 'pods_genre' field from the custom post type 'films'.
 */
add_action( 'pods_api_post_save_pod_item_films', 'my_tax_update', 10, 3 );
function my_tax_update( $pieces, $is_new_item, $id ) {
    // get the values of the 'pods_genre' field
    $terms = $pieces[ 'fields' ][ 'ingredients_relation_field' ][ 'value' ];
    if ( empty( $terms ) ) {
        // if there is nothing there set $term to null to avoid errors
        $terms = null;
    } else {
        if ( ! is_array($terms) ) {
            // create an array out of the comma separated values
            $terms = explode(',', $terms);
        }
        
        // ensure all values in the array are integer (otherwise the numerical value is taken as a new taxonomy term instead of an ID)
            $terms = array_map('intval', $terms);
    }
    // Set the term for taxonomy 'genre' with $terms
    wp_set_object_terms( $id, $terms, 'ingrediente', false );
    
}

I made an export of the two pods, I thought it might be useful.

{
    "@meta": {
        "version": "2.9.10.2",
        "build": 1671878070
    },
    "settings": {
        "types_only": "0",
        "watch_changed_fields": "0",
        "metadata_integration": "1",
        "metadata_override_get": "0",
        "session_auto_start": "0",
        "wisdom_opt_out": "1"
    },
    "pods": [
        {
            "name": "ingrediente",
            "id": 7,
            "label": "Ingredienti",
            "description": "",
            "type": "taxonomy",
            "storage": "meta",
            "label_singular": "Ingrediente",
            "public": "1",
            "show_ui": "1",
            "hierarchical": "1",
            "rest_enable": "1",
            "_migrated_28": "1",
            "built_in_post_types_ricetta": "1",
            "groups": [
                {
                    "name": "campi_aggiuntivi",
                    "id": 8,
                    "label": "Campi aggiuntivi",
                    "description": "",
                    "weight": 0,
                    "fields": []
                }
            ]
        },
        {
            "name": "ricetta",
            "id": 5,
            "label": "Ricette",
            "description": "",
            "type": "post_type",
            "storage": "meta",
            "label_singular": "Ricetta",
            "public": "1",
            "show_ui": "1",
            "rest_enable": "1",
            "supports_title": "1",
            "supports_editor": "1",
            "_migrated_28": "1",
            "built_in_taxonomies_ingrediente": "1",
            "required": "0",
            "groups": [
                {
                    "name": "campi_aggiuntivi",
                    "id": 6,
                    "label": "Campi aggiuntivi",
                    "description": "",
                    "weight": 0,
                    "fields": [
                        {
                            "name": "ingredients_relation_field",
                            "id": 9,
                            "label": "ingredients_relation_field",
                            "description": "",
                            "weight": 0,
                            "type": "pick",
                            "pick_object": "taxonomy",
                            "pick_val": "ingrediente",
                            "sister_id": "-- Seleziona --",
                            "pick_table": "-- Seleziona --",
                            "required": "0",
                            "pick_format_type": "single",
                            "pick_format_single": "dropdown",
                            "pick_format_multi": "list",
                            "pick_display_format_multi": "default",
                            "pick_display_format_separator": ", ",
                            "pick_allow_add_new": "1",
                            "pick_taggable": "0",
                            "pick_show_icon": "1",
                            "pick_show_edit_link": "1",
                            "pick_show_view_link": "1",
                            "pick_limit": "0",
                            "pick_user_role": "Administrator",
                            "pick_post_status": "publish",
                            "pick_post_author": "0",
                            "repeatable": "0",
                            "repeatable_format": "default",
                            "roles_allowed": "administrator",
                            "rest_pick_response": "array",
                            "rest_pick_depth": "1"
                        }
                    ]
                }
            ]
        }
    ]
}

May be some one can see what I’m doing wrong, thanks in advance.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer