Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

There are two flaws in the code that I can see. The first one is a bug in this code

if($post->post_type != 'product')

If no post exists, you get the following error

Notice: Trying to get property of non-object...

This can be fixed by first checking if a post isset

if(isset($post) && $post->post_type != 'product')

Secondly, wp_set_post_terms() should not be used for custom taxonomies on custom post types, the correct function should be wp_set_object_terms(). From the codex page

This function will only work on the native post type. For a taxonomy on a custom post type use wp_set_object_terms()

So, your code should look something like this

add_action('save_post', 'assign_parent_terms');

function assign_parent_terms($post_id){
global $post;


if(isset($post) && $post->post_type != 'event_type')
    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_object_terms($post_id, array($term->parent), 'product_cat', true);
        $term = get_term($term->parent, 'product_cat');
    }
  }
}
share|improve this answer
1  
This absolutely fixed my problem!!! Many thanks Pieter! –  Traveler Jul 9 at 9:55
    
My pleasure. Glad I could help –  Pieter Goosen Jul 9 at 12:15
    
@PieterGoosen: lets just use the second argument for the hook which is the $post object and remove the global. I updated my answer. –  Sisir Jul 11 at 4:21
    
@Sisir thanks for the update –  Pieter Goosen Jul 11 at 4:56

save_post is actually called on saving a post through the Quick Edit feature as well. As we find from the source code, the save_post action is called from two functions: wp_publish_post and wp_insert_post, the latter being called both when saving a post through the normal process and when saving a post through the quick edit process!

So, the code as you currently have it should work for Quick Edit as well!

share|improve this answer
    
Its not working for me, when I use the quick edit with the code above, the parent term is not added automatically when the sub term is selected. –  Traveler Jul 8 at 16:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.