April 11, 2018
I’ve always wondered how to do this, and now I have an example on hand that I can refer to in the future. Comes in handy if you’re using custom navigation / filters with categories or terms.
$tax ='taxonomy'; $args = array( 'taxonomy' => $tax, 'hide_empty' => false, 'parent' => 0, ); $toplv_term_query = new WP_Term_Query($args); foreach($toplv_term_query->get_terms() as $toplv_term){ $args = array( 'taxonomy' => $tax, 'hide_empty' => false, 'parent' => $toplv_term->term_id, ); $secondlv_term_query = new WP_Term_Query($args); foreach($secondlv_term_query->get_terms() as $secondlv_term){ $secondlv_term->term_id; } }
Sourced here.