248 | | // Put checked cats on top |
| 248 | // Add the full hierarchy of the checked categories |
| 249 | foreach ( $checked_categories as $cat ) { |
| 250 | // Get the checked category ancestor |
| 251 | $cat_parent = $cat; |
| 252 | while ( $cat_parent->parent != 0 ) { |
| 253 | $cat_parent = get_term_by( 'id', $cat_parent->parent, $taxonomy ); |
| 254 | } |
| 255 | |
| 256 | // Include the ancestor |
| 257 | if ( ! array_key_exists( $cat_parent->term_id, $checked_categories ) ) { |
| 258 | $checked_categories[$cat_parent->term_id] = $cat_parent; |
| 259 | } |
| 260 | |
| 261 | // Include the children categories of the ancestor |
| 262 | $children_cats = get_terms( $taxonomy, array( |
| 263 | 'child_of' => $cat_parent->term_id, |
| 264 | 'fields' => 'ids', |
| 265 | 'hide_empty' => 0, |
| 266 | ) ); |
| 267 | if ( $children_cats ) { |
| 268 | foreach ( $children_cats as $child_cat_id ) { |
| 269 | $child_cat = get_term_by( 'id', $child_cat_id, $taxonomy ); |
| 270 | if ( ! array_key_exists( $child_cat->term_id, $checked_categories ) ) { |
| 271 | $checked_categories[$child_cat->term_id] = $child_cat; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Unset the newly found categories to avoid duplication |
| 278 | foreach ( $categories as $key => $category ) { |
| 279 | if ( array_key_exists( $category->term_id, $checked_categories ) ) { |
| 280 | unset( $categories[$key] ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Put checked cats and their hierarchy on top |