It may happen that when you save your post, you want it to be considered not only in the chosen category but also in its parent category.
For example, you have the following categories:
– Auto
– – BMW
– – Audi
By selecting Audi you also automatically want to have the Auto category. To do this just use the save_post hook
Add automatic parent category
add_action ("save_post," "cf_save_parent_category");
function cf_save_parent_category ($postid)
get parent post id
$parent-post-id - get_post ($postid)-post_parent;
$parent -post-id ($parent-post-id!)? $parent-post-id: $postid;
$current-categories - wp_get_post_categories ($parent-post-id);
foreach ($current-categories as $key ' '$category'id)
$ancestor-category - get_ancestors ($category-id, 'category');
foreach ($ancestor-category as $ancestor-category-id)
$current-categories[]-$ancestor-category-id;
}
}
wp_set_post_categories ($parent-post-id, $current-categories);
$current-categories - wp_get_post_categories ($parent-post-id);
}