Skip to content

Category

Namespace: DMS\Listing\Category Obtained via: dms_get_category(), dms_get_all_categories()

Represents a single listing category (a grouping of terms, e.g. "Make", "Model", "Color"). Provides typed accessors for its terms and settings.


Core

get_id(): int

Returns the category ID.

$id = $category->get_id();

exists(): bool

Returns true if the category was found.

if ( $category->exists() ) { ... }

get( string $name, mixed $default = null ): mixed

Returns a category property or setting value. Checks direct properties first, then the settings array.

Direct properties: id, type_id, singular, plural, slug, order_id Common settings keys: is_filterable, is_location, order, category_type

echo esc_html( $category->get( 'singular' ) );  // e.g. "Make"
echo esc_html( $category->get( 'plural' ) );    // e.g. "Makes"
echo esc_html( $category->get( 'slug' ) );      // e.g. "make"
echo $category->get( 'type_id' );

set( $name, $value ): bool|int

Updates a category property or setting and persists to the database. Direct properties (singular, plural, slug, order_id) update their own column; everything else is stored in the serialized settings column.

$category->set( 'plural', 'Makes' );
$category->set( 'is_filterable', 1 );

delete(): bool

Deletes the category and all of its terms.

$category->delete();

get_all_data(): array

Returns a plain array of all category data including id, type_id, singular, plural, slug, and settings.

$data = $category->get_all_data();

Terms

get_terms( array $args = [] ): array

Returns all Term objects for this category. Supports filtering, pagination, and search.

Arg key Type Description
search string Filter terms by value or slug
remove_empty bool Exclude terms with a total of 0
only_empty bool Return only terms with a total of 0
load_term_count bool Load accurate counts from the DB
page int Page number (used with per_page)
per_page int Items per page
$terms = $category->get_terms();

foreach ( $terms as $term ) {
    echo esc_html( $term->get( 'term' ) );
}

// Search
$results = $category->get_terms( [ 'search' => 'Toyota' ] );

// Paginated
$page_terms = $category->get_terms( [ 'page' => 1, 'per_page' => 20 ] );

count_terms( array $args = [] ): int

Returns the total number of terms without loading Term objects.

$total = $category->count_terms();
$found = $category->count_terms( [ 'search' => 'toy' ] );

has_term( $value, $column = 'term' ): bool|Term

Checks whether a term with the given value exists in this category. Returns the Term object if found, false otherwise.

$term = $category->has_term( 'Toyota' );

if ( $term ) {
    echo $term->get_id();
}

// Check by slug
$term = $category->has_term( 'toyota', 'slug' );

add_term( array $term_data ): int

Creates a new term within this category. Returns the new term ID.

$term_id = $category->add_term( [
    'term'  => 'Toyota',
    'total' => 0,
] );