# Displaying only used categories in filter

**URL:** <https://community.hivepress.io/t/displaying-only-used-categories-in-filter/10892>\
**Category:** HivePress\
**Created:** [May 15, 2024, 6:40pm UTC](https://community.hivepress.io/t/displaying-only-used-categories-in-filter/10892 "2024-05-15T18:40:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apos37](https://community.hivepress.io/user_avatar/community.hivepress.io/apos37/32/967_2.png) [@apos37](https://community.hivepress.io/u/apos37)\
**Post date:** [May 15, 2024, 6:40pm UTC](https://community.hivepress.io/t/displaying-only-used-categories-in-filter/10892/1 "2024-05-15T18:40:32Z")

</div>

Is it possible to hide categories from the listings search page filter where there are no listings with those categories yet? It is annoying to try to filter a category and find nothing until there is a listing added that uses it.

---

<div class="post-metadata">

**Author:** ![apos37](https://community.hivepress.io/user_avatar/community.hivepress.io/apos37/32/967_2.png) [@apos37](https://community.hivepress.io/u/apos37)\
**Post date:** [May 15, 2024, 7:38pm UTC](https://community.hivepress.io/t/displaying-only-used-categories-in-filter/10892/2 "2024-05-15T19:38:41Z")

</div>

Nevermind, I figured it out:

For listings:

```php
add_filter( 'hivepress/v1/forms/listing_filter', function( $form ) {
    $categories = $form['fields']['_category']['options'];
    foreach ( $categories as $term_id => $category ) {
        if ( $term_id == 0 ) {
            continue;
        }
        $term = get_term( $term_id, 'hp_listing_category' );
        $count = $term->count;
        if ( $count == 0 ) {
            unset( $form['fields']['_category']['options'][$term_id] );
        }
    }
    return $form;
}, 1000 );

```

And for vendors:

```php
add_filter( 'hivepress/v1/forms/vendor_filter', function( $form ) {
    $categories = $form['fields']['_category']['options'];
    foreach ( $categories as $term_id => $category ) {
        if ( $term_id == 0 ) {
            continue;
        }
        $args = [
            'post_type' => 'hp_vendor',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'tax_query' => [
                  'relation' => 'AND',
                  [
                      'taxonomy' => 'hp_vendor_category',
                      'field' => 'id',
                      'terms' => [$term_id]
                  ]
            ]
        ];
        $query = new WP_Query( $args );
        $count = (int)$query->post_count;
        wp_reset_postdata();

        if ( $count == 0 ) {
            unset( $form['fields']['_category']['options'][$term_id] );
        }
    }
    return $form;
}, 1000 );

```

---

<div class="post-metadata">

**Author:** ![system](https://community.hivepress.io/user_avatar/community.hivepress.io/system/32/13990_2.png) [@system](https://community.hivepress.io/u/system)\
**Post date:** [June 14, 2024, 7:39pm UTC](https://community.hivepress.io/t/displaying-only-used-categories-in-filter/10892/3 "2024-06-14T19:39:11Z")

</div>

Closed on June 14, 2024, 7:39pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
