# Creating models

If you are developing a custom HivePress [extension](https://docs.hivepress.io/developer-docs/tutorials/create-a-custom-hivepress-extension), you may need to create a new model. To do this, create a new `class-{model-name}.php` file (use lowercase letters, numbers, and hyphens only) in the `includes/models` extension subdirectory and HivePress will load it automatically.

Then, define the model PHP class. The class name should be based on the file name but with underscores instead of hyphens and no lowercase restriction (e.g. `Foo_Bar` class for the `class-foo-bar.php` file). Pick a name that is unique enough to avoid conflicts with other HivePress models.

```php
<?php
namespace HivePress\Models;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

class Foo_Bar extends Post {
	public function __construct( $args = [] ) {
		$args = hp\merge_arrays(
			[
				// Define the model fields.
				'fields' => [
					'description' => [
						'type'       => 'textarea',
						'max_length' => 1234,
						'required'   => true,
						'_alias'     => 'post_content',
					],
				],
			],
			$args
		);

		parent::__construct( $args );
	}

	// Implement custom methods.
	public function get_short_description() {
		return substr( $this->get_description(), 0, 123 );
	}
}
```

The code example above implements a `Foo_Bar` model based on the `Post` model. This means that the `Foo_Bar` objects are stored as posts of a custom `hp_foo_bar` type (the same `hp_` prefix is used for comment types and taxonomies). A model class can be based on one of the base classes or any other model class. The following base classes are available:

* **Post** - for models based on a custom post type (e.g. `Listing`);
* **Term** - for models based on a custom taxonomy (e.g. `Listing_Category`);
* **Comment** - for models based on a custom comment type (e.g. `Review`).

{% hint style="info" %}
If your model inherits the `Post` class, you must register the `hp_{model_name}` post type separately, either via the [WordPress API](https://developer.wordpress.org/reference/functions/register_post_type/) or [HivePress configuration](https://docs.hivepress.io/developer-docs/framework/configurations/post-types). Similarly, if your model inherits the `Term` class, you must register a taxonomy.
{% endhint %}

The `Foo_Bar` model has a single required `description` [field](https://docs.hivepress.io/developer-docs/framework/fields) mapped to the standard `post_content` field (so the value will be stored in the `post_content` column of the `wp_posts` database table). Also, there's a `get_short_description` method used as a helper for getting the short description limited to 123 characters.

In the same way, you can create custom models for your HivePress extension.
