wc_reorder_terms( int $the_term, int $next_id, string $taxonomy, int $index, mixed $terms = null )
Move a term before the a given element of its hierarchy level.
Description Description
Parameters Parameters
- $the_term
-
(Required) Term ID.
- $next_id
-
(Required) The id of the next sibling element in save hierarchy level.
- $taxonomy
-
(Required) Taxnomy.
- $index
-
(Required) Term index (default: 0).
- $terms
-
(Optional) List of terms. (default: null).
Default value: null
Return Return
(int)
Source Source
File: includes/wc-term-functions.php
function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) {
if ( ! $terms ) {
$terms = get_terms( $taxonomy, 'hide_empty=0&parent=0&menu_order=ASC' );
}
if ( empty( $terms ) ) {
return $index;
}
$id = intval( $the_term->term_id );
$term_in_level = false; // Flag: is our term to order in this level of terms.
foreach ( $terms as $term ) {
$term_id = intval( $term->term_id );
if ( $term_id === $id ) { // Our term to order, we skip.
$term_in_level = true;
continue; // Our term to order, we skip.
}
// the nextid of our term to order, lets move our term here.
if ( null !== $next_id && $term_id === $next_id ) {
$index++;
$index = wc_set_term_order( $id, $index, $taxonomy, true );
}
// Set order.
$index++;
$index = wc_set_term_order( $term_id, $index, $taxonomy );
/**
* After a term has had it's order set.
*/
do_action( 'woocommerce_after_set_term_order', $term, $index, $taxonomy );
// If that term has children we walk through them.
$children = get_terms( $taxonomy, "parent={$term_id}&hide_empty=0&menu_order=ASC" );
if ( ! empty( $children ) ) {
$index = wc_reorder_terms( $the_term, $next_id, $taxonomy, $index, $children );
}
}
// No nextid meaning our term is in last position.
if ( $term_in_level && null === $next_id ) {
$index = wc_set_term_order( $id, $index + 1, $taxonomy, true );
}
return $index;
}