# POSTMETA

### Postmeta

#### What is Metadata?

Metadata can be described as data about data. Postmeta is the way to store post-related metadata that has an impact on a post. All the data is stored in the postmeta table and mapped with the post\_id.&#x20;

#### What is stored in the metadata tables? For example:

* For posts, post\_type is an example of metadata, but it's stored in the posts table
* Taxonomy terms, categories, and tags can also be loosely defined as metadata, but these are stored entirely separately, in their own database tables.
* Post metadata such as custom fields and additional metadata added via extension is stored in the postmeta table, as you would expect.

So it's easier to think of metadata in miniCal not according to the strict definition of the term, but as the data stored in the three metadata tables.

#### But let's have a look at some of the more common types of metadata:

* **Metadata about the extension.**
* **Custom Fields.**
* **User Metadata.**&#x20;

### Structure of the miniCal post-meta Table

* `post_id` => //post id
* `post_key` => // key
* `post_value` => // value

#### Add postmeta data with post data

You can add postmeta data with post data, Just add an array with key name meta. Here is an example is given.

```php
    $post = array(  
             'company_id' => $this->company_id,
             'user_id' => $this->user_id,
             'post_title' => 'Book',
             'post_type' => 'self-help-book',
             'post_date' =>  date('Y-m-d H:i:s'),
             'meta' => array(
                     'author' => 'James Clear',
                     'book_name' => 'Atomic Habits',
                     'pages' => '290'
                 )
    );
    
    add_post($post);
```

```php
// Adds meta values to the database
add_post_meta ($post_id, $key, $value);

// to get postmeta data from postmeta table by post_id or meta_key.
get_post_meta(int $post_id = null, string $key = null, bool $single = false );

// update postmeta data. in the array, if a new value pass then added else update.
 update_post_meta(array $post_meta = null, int $post_id = null);

//delete a postmeta data.
delete_post_meta(int $post_id = null, string $meta_key = null, $meta_value = null);
```

###

<table><thead><tr><th width="239">Method</th><th>Description</th><th>Parameters Description</th><th>Return value</th></tr></thead><tbody><tr><td><code>add_post_meta()</code></td><td>Adds a new postmeta value.</td><td><strong><code>add_post_meta ($post_id, $key, $value)</code></strong><br><br><strong>1.</strong> int $post_id (Required) The id of the post corresponds to the postmeta data.<br><strong>2.</strong> string $key (Required) The meta key.<br><strong>3.</strong> mixed $value (Required) The meta value to the corresponding key.<br></td><td><strong>null</strong><br></td></tr><tr><td><code>get_postmeta()</code></td><td>Get postmeta value.</td><td><strong><code>get_post_meta(int $post_id = null, string $key = null, bool $single = false )</code></strong><br><br><strong>1.</strong> int $post_id The id of the post corresponds to the postmeta data.<br><strong>2.</strong> string $key Optional, The meta key to retrieve. By default, returns data for all keys.<br><strong>3.</strong> boolean $single Optional Whether to return a single value. This parameter has no effect if $key is not specified.<br></td><td><strong>mixed</strong><br>Either array or null, if postmeta data is available or no error occurs, then array else null.</td></tr><tr><td><code>update_postmeta()</code></td><td>Update postmeta value.</td><td><strong><code>update_post_meta(array $post_meta = null, int $post_id = null)</code></strong><br><br><strong>1.</strong> array $post_meta An array of postmeta data to be updated.<br><strong>2.</strong> int $post_id The id of the post corresponds to the postmeta data.<br></td><td><strong>mixed</strong><br>Either true or null, if postmeta data is updated then true else null.</td></tr><tr><td><code>detele_postmeta()</code></td><td>Detele postmeta value.</td><td><strong><code>delete_post_meta(int $post_id = null, string $meta_key = null, $meta_value = null)</code></strong><br><br><strong>1.</strong> int $post_id The id of the post corresponds to the postmeta data.<br><strong>2.</strong> string $meta_key name of key.<br><strong>3.</strong> mixed $meta_value value of key this will make delete more certain (in case key name is same).<br></td><td><strong>mixed</strong><br>Either true or null, if postmeta data is deleted then true else null.</td></tr></tbody></table>


---

# 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://docs.minical.io/build-an-extension/store-custom-data/postmeta.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.
