'adqs_category', 'hide_empty' => false, 'hierarchical' => true, 'parent' => 0, ]; $args = array_merge( $default_args, $args ); $taxonomy = $args['taxonomy'] ?? 'adqs_category'; // Check if the taxonomy exists, if not return empty if ( ! taxonomy_exists( $taxonomy ) ) { return ''; } $terms = get_terms( $args ); if ( $terms && ! is_wp_error( $terms ) ) { // Filter terms by post types if filter_by_post_types is provided if ( ! empty( $options['filter_by_post_types'] ) && is_array( $options['filter_by_post_types'] ) ) { // Query posts to find which terms are actually used $posts_query = new \WP_Query( [ 'post_type' => $options['filter_by_post_types'], 'posts_per_page' => -1, 'fields' => 'ids', 'post_status' => 'publish', ] ); if ( $posts_query->have_posts() ) { // Get term IDs used by these posts $used_term_ids = []; foreach ( $posts_query->posts as $post_id ) { $post_terms = wp_get_post_terms( $post_id, $taxonomy, [ 'fields' => 'ids' ] ); if ( ! is_wp_error( $post_terms ) && ! empty( $post_terms ) ) { $used_term_ids = array_merge( $used_term_ids, $post_terms ); } } $used_term_ids = array_unique( $used_term_ids ); // Filter terms to only those used by the posts $terms = array_filter( $terms, function ( $term ) use ( $used_term_ids ) { return in_array( $term->term_id, $used_term_ids, true ); } ); } else { // No posts found, return empty terms $terms = []; } wp_reset_postdata(); } // Filter terms by directory if has_directory is enabled if ( $has_directory && ! empty( $directory_id ) ) { $terms = array_filter( $terms, function ( $term ) use ( $directory_id ) { $term_directories = adqs_get_term_directories( $term->term_id ); // If term has no directory assignments, show it (backward compatibility) if ( empty( $term_directories ) ) { return true; } // Otherwise, only show if it's assigned to the current directory return in_array( $directory_id, $term_directories, true ); } ); } foreach ( $terms as $term ) { // Ensure $term is a valid WP_Term object (like core function does) if ( ! is_object( $term ) || ! isset( $term->term_id ) || ! isset( $term->name ) ) { continue; } $checked = in_array( $term->term_id, $saved_data, true ) ? 'checked' : ''; $child_terms = get_term_children( $term->term_id, $taxonomy ); ?>
  • term_id, 'term_icon_id', true ); $icon = ! empty( $icon ) ? "" : ''; ?> name ); ?> (count ) ? esc_html( $term->count ) : '0'; ?>)