> For the complete documentation index, see [llms.txt](https://docs.woohoo.dance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.woohoo.dance/ajax-filters/for-developers.md).

# For developers

#### `woohoo_loop_total_posts`

Filter the total post count used by the Ajax Query Count element and pagination.

```php
apply_filters( 'woohoo_loop_total_posts', int $total_posts, WP_Query $loop, array $args );
```

**Parameters:**

| Parameter      | Type       | Description                                                             |
| -------------- | ---------- | ----------------------------------------------------------------------- |
| `$total_posts` | `int`      | The computed total post count                                           |
| `$loop`        | `WP_Query` | The query object (post\_\_in may have been modified by pre\_get\_posts) |
| `$args`        | `array`    | The original query arguments passed to WP\_Query                        |

**Example — Variations as cards:**

When your shop archive shows each WooCommerce variation as its own card, Woohoo counts parent products but your loop displays variations. This filter lets you return the correct count:

```php
add_filter( 'woohoo_loop_total_posts', function( $total_posts, $loop, $args ) {
    $post_type = $loop->get( 'post_type' );
    if ( ! is_array( $post_type ) || ! in_array( 'product_variation', $post_type, true ) ) {
        return $total_posts;
    }
    $post_in = $loop->get( 'post__in' );
    if ( empty( $post_in ) || $post_in === [ 0 ] ) {
        return $total_posts;
    }
    return count( $post_in );
}, 10, 3 );
```

#### `woohoo_facet_term_counts`

Filter facet term counts before they are displayed. Runs after counts are computed and formatted, but before exclusions and ordering.

```php
apply_filters( 'woohoo_facet_term_counts', array $result, string $target_slug, string $target_tax, array $base_ids, bool $is_product_attribute );
```

**Parameters:**

| Parameter               | Type     | Description                                                                        |
| ----------------------- | -------- | ---------------------------------------------------------------------------------- |
| `$result`               | `array`  | Array of term data, each with `slug`, `name`, `count`, etc.                        |
| `$target_slug`          | `string` | The facet slug (e.g. `"serie"`)                                                    |
| `$target_tax`           | `string` | The taxonomy name (e.g. `"product_cat"`, `"pa_colore"`)                            |
| `$base_ids`             | `array`  | Post IDs currently in the filtered result set                                      |
| `$is_product_attribute` | `bool`   | `true` for WooCommerce product attributes (`pa_*`), `false` for regular taxonomies |

**Example — Multiply taxonomy counts by variation count:**

When showing variations as cards, taxonomy facets (Category, Serie) count parent products. But your shop displays N cards per product (one per variation). This filter multiplies the count:

```php
add_filter( 'woohoo_facet_term_counts', function( $result, $slug, $tax, $base_ids, $is_attr ) {
    // Attribute facets (pa_colore) already have correct counts — skip them.
    if ( $is_attr || empty( $result ) ) {
        return $result;
    }

    foreach ( $result as $i => $term ) {
        // Your logic to multiply count by variations per product.
        // See woohoo-variations-as-cards plugin for a full implementation.
    }

    return $result;
}, 10, 5 );
```

#### `woohoo_filters/indexer/get_rows`

Filter the index rows before they are inserted into the Woohoo index table. Use this to add extra rows for data that Woohoo doesn't index by default.

```php
apply_filters( 'woohoo_filters/indexer/get_rows', array $rows, int $object_id, array $facet );
```

**Example — Index variation attributes on the parent product:**

WooCommerce stores attributes like `pa_colore` on variations, not on the parent product. Without this, the Color facet returns no results for variable products:

```php
add_filter( 'woohoo_filters/indexer/get_rows', function( $rows, $object_id, $facet ) {
    $source = $facet['source'] ?? '';
    if ( strpos( $source, 'attribute/' ) !== 0 ) {
        return $rows;
    }
    $taxonomy = 'pa_' . str_replace( 'attribute/', '', $source );
    $product  = wc_get_product( $object_id );
    if ( ! $product || ! $product->is_type( 'variable' ) ) {
        return $rows;
    }
    foreach ( $product->get_children() as $variation_id ) {
        $terms = wp_get_object_terms( $variation_id, $taxonomy );
        foreach ( $terms as $term ) {
            $rows[] = [
                'object_id'    => $object_id,
                'slug'         => $facet['slug'],
                'facet_value'  => $term->slug,
                'facet_name'   => $term->name,
                'facet_id'     => $term->term_id,
                'facet_parent' => $term->parent,
                'facet_order'  => $term->term_order ?? 0,
            ];
        }
    }
    return $rows;
}, 10, 3 );
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.woohoo.dance/ajax-filters/for-developers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
