> For the complete documentation index, see [llms.txt](https://ce-docs.keywordrush.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ce-docs.keywordrush.com/faq/how-to-add-offer-count-as-a-custom-field.md).

# How to add offer count as a custom field

**Update:** Content Egg now stores the total number of offers in the `_cegg_offers_count` meta field by default.

You can read more about this feature here:

[Offers Count (Badge & Shortcodes)](/woocommerce/offers-count-badge-and-shortcodes.md)

***

Add the following code to the `functions.php` file of your theme (preferably the child theme) to ensure future updates don’t overwrite your changes.

```php
add_action('content_egg_save_data', 'add_offer_count_in_custom_field', 20, 4);

function add_offer_count_in_custom_field($data, $module_id, $post_id, $is_last_iteration) {
    if (!$is_last_iteration) {
        return;
    }

    $field_name = 'offercount';  // Change this to your custom field name
    $data = \ContentEgg\application\components\ContentManager::getViewProductData($post_id);

    // Filter out items where 'stock_status' is not set or is -1
    $filtered_data = array_filter($data, function ($item) {
        return isset($item['stock_status']) && $item['stock_status'] != -1;
    });

    \update_post_meta($post_id, $field_name, count($filtered_data));
}

```

Note: The custom field will be updated when posts containing Content Egg offers are created or updated.


---

# 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://ce-docs.keywordrush.com/faq/how-to-add-offer-count-as-a-custom-field.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.
