# 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.&#x20;

You can read more about this feature here:

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

***

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: 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.
