0

When post is saved (not real time toggling). The list of locations category isn't displayed correctly

function assign_parent_terms($post_id, $post) {
    $temp_taxonomies = get_post_taxonomies($post);
    foreach ($temp_taxonomies as $temp_tax) {

        $terms = get_the_terms($post_id, $temp_tax );

        if ( ! empty($terms)) {

            foreach($terms as $term) {

                // note, if wanting to auto-select parents in all categories, replace all "w2dc-location" with $temp_tax

                while($term->parent != 0 && !has_term( $term->parent, 'w2dc-location', $post )) {

                    /*

                    echo '<script language="javascript">';

                    echo 'alert("in here")';

                    echo '</script>';

                    */

                    // move upward until we get to 0 level terms

                    wp_set_post_terms($post_id, array($term->parent), 'w2dc-location', true);

                    $term = get_term($term->parent, 'w2dc-location');

                    wp_set_object_terms($post_id, $term->ID, 'w2dc-location', true);
                }
            }
        }
    }
}
add_action('save_post', 'assign_parent_terms', 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.