I found this solution to automatically assign parent terms when a child term is selected, which is working fine for the save_post hook:

add_action('save_post', 'assign_parent_terms');
function assign_parent_terms($post_id){
global $post;
if($post->post_type != 'product')
    return $post_id;
// get all assigned terms   
$terms = wp_get_post_terms($post_id, 'product_cat' );
foreach($terms as $term){
    while($term->parent != 0 && !has_term( $term->parent, 'product_cat', $post )){
        // move upward until we get to 0 level terms
        wp_set_post_terms($post_id, array($term->parent), 'product_cat', true);
        $term = get_term($term->parent, 'product_cat');
    }
  }
}

As I have a large number of posts, I need to hook into the quick edit action, not just into the save-post action.

Anybody has a solution of this hook?

Read more here: How to hook into the quick edit action?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , ,

Wordpress related questions and answers: