> For the complete documentation index, see [llms.txt](https://docs.hivepress.io/developer-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hivepress.io/developer-docs/framework/models/creating-models.md).

# Creating models

If you are developing a custom HivePress [extension](/developer-docs/tutorials/create-a-custom-hivepress-extension.md), 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](/developer-docs/framework/configurations/post-types.md). Similarly, if your model inherits the `Term` class, you must register a taxonomy.
{% endhint %}

The `Foo_Bar` model has a single required `description` [field](/developer-docs/framework/fields.md) 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.


---

# 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, and the optional `goal` query parameter:

```
GET https://docs.hivepress.io/developer-docs/framework/models/creating-models.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
