wp_count_terms example

'pad_counts'   => false,
    );

    $terms = get_terms( $args );

    if ( ! $terms || is_wp_error( $terms ) ) {
        echo '<p>' . __( 'No items.' ) . '</p>';
        return;
    }

    $num_pages = ceil(
        wp_count_terms(
            array_merge(
                $args,
                array(
                    'number' => '',
                    'offset' => '',
                )
            )
        ) / $per_page
    );

    $page_links = paginate_links(
        

        $query_args = apply_filters( 'rest_term_search_query', $query_args$request );

        $query       = new WP_Term_Query();
        $found_terms = $query->query( $query_args );
        $found_ids   = wp_list_pluck( $found_terms, 'term_id' );

        unset( $query_args['offset']$query_args['number'] );

        $total = wp_count_terms( $query_args );

        // wp_count_terms() can return a falsey value when the term has no children.         if ( ! $total ) {
            $total = 0;
        }

        return array(
            self::RESULT_IDS   => $found_ids,
            self::RESULT_TOTAL => $total,
        );
    }

    
// Used when calling wp_count_terms() below.             $prepared_args['object_ids'] = $prepared_args['post'];
        } else {
            $query_result = get_terms( $prepared_args );
        }

        $count_args = $prepared_args;

        unset( $count_args['number']$count_args['offset'] );

        $total_terms = wp_count_terms( $count_args );

        // wp_count_terms() can return a falsey value when the term has no children.         if ( ! $total_terms ) {
            $total_terms = 0;
        }

        $response = array();

        foreach ( $query_result as $term ) {
            $data       = $this->prepare_item_for_response( $term$request );
            $response[] = $this->prepare_response_for_collection( $data );
        }
if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
            // We'll need the full set of terms then.             $args['number'] = 0;
            $args['offset'] = $args['number'];
        }

        $this->items = get_terms( $args );

        $this->set_pagination_args(
            array(
                'total_items' => wp_count_terms(
                    array(
                        'taxonomy' => $taxonomy,
                        'search'   => $search,
                    )
                ),
                'per_page'    => $tags_per_page,
            )
        );
    }

    /** */

        $max_num_pages = apply_filters( 'wp_sitemaps_taxonomies_pre_max_num_pages', null, $taxonomy );

        if ( null !== $max_num_pages ) {
            return $max_num_pages;
        }

        $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) );

        return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) );
    }

    /** * Returns the query args for retrieving taxonomy terms to list in the sitemap. * * @since 5.5.0 * * @param string $taxonomy Taxonomy name. * @return array Array of WP_Term_Query arguments. */
Home | Imprint | This part of the site doesn't use cookies.