• Resolved rico34

    (@rico34)


    Hi,

    I want to sync the value of a relationship field of a custom post with the associated custom taxonomy terms.
    I have read that in the past you would have to use some custom code to achieve this but now it says in your documentation that this is no longer needed since 3.2.4+
    (https://docs.pods.io/code-snippets/update-taxonomy-value-stored-relationship-field/)

    I found the option field ‘Sync associated taxonomy with this relationship’ in the ‘Relationship Options’ of the regarding relationship field. But it is just an empty plain text field. I didn’t find an explanation on how to use it. So how to get this to work by using this field?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi, I was wondering about this too. No matter what value I put there, the sync does not work. Did you find a solution?

    Thread Starter rico34

    (@rico34)

    Hi,

    yes, but I went the custom code way and did not use the pods option field for that because I couldn’t find a way to get it working by using it.

    Here’s the code I’ve used:

    add_action('save_post', 'sync_relationship_field_with_taxonomy', 10, 3);

    function sync_relationship_field_with_taxonomy($post_id) {

    if (get_post_type($post_id) != 'your_post_type_name') {

    return;

    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    if (wp_is_post_revision($post_id)) return;

    $relationship_field = get_post_meta($post_id, 'your_pods_custom_field', true);

    if (!empty($relationship_field)) {

    // Sync the term to the taxonomy
    $result = wp_set_post_terms($post_id, array($relationship_field), 'your_custom_taxonomy');

    } else {

    // Clear the taxonomy terms if no term is selected
    $result = wp_set_post_terms($post_id, array(), 'your_custom_taxonomy')

    }

    }


    In my case I am using a custom field with radio buttons because I want the user to be able to choose only one option. So only one term of the custom taxonomy should be set.
    This code works for my case. But I’m not sure if this is the best way to do that or if there are some flaws in that. I’m still testing things out.

    Thanks for your reply and sharing your code! Guess this will do the job ?? However, I’d be keen to find out how to get the original feature working. Anyone of the PODS team who can help?

    Plugin Contributor Scott Kingsley Clark

    (@sc0ttkclark)

    Looking into this for the release I’m planning this week.

    Thanks Scott –?that’s awesome! Looking forward to the fix!

    Plugin Support Paul Clark

    (@pdclark)

    A pending pull request for the Taxonomy Sync feature can be found at https://github.com/pods-framework/pods/pull/7336

    Plugin Contributor Scott Kingsley Clark

    (@sc0ttkclark)

    The fix went out for this this past week.

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