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.

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.

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.

    $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);
// 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);

MethodDescriptionParameters DescriptionReturn value

add_post_meta()

Adds a new postmeta value.

add_post_meta ($post_id, $key, $value) 1. int $post_id (Required) The id of the post corresponds to the postmeta data. 2. string $key (Required) The meta key. 3. mixed $value (Required) The meta value to the corresponding key.

null

get_postmeta()

Get postmeta value.

get_post_meta(int $post_id = null, string $key = null, bool $single = false ) 1. int $post_id The id of the post corresponds to the postmeta data. 2. string $key Optional, The meta key to retrieve. By default, returns data for all keys. 3. boolean $single Optional Whether to return a single value. This parameter has no effect if $key is not specified.

mixed Either array or null, if postmeta data is available or no error occurs, then array else null.

update_postmeta()

Update postmeta value.

update_post_meta(array $post_meta = null, int $post_id = null) 1. array $post_meta An array of postmeta data to be updated. 2. int $post_id The id of the post corresponds to the postmeta data.

mixed Either true or null, if postmeta data is updated then true else null.

detele_postmeta()

Detele postmeta value.

delete_post_meta(int $post_id = null, string $meta_key = null, $meta_value = null) 1. int $post_id The id of the post corresponds to the postmeta data. 2. string $meta_key name of key. 3. mixed $meta_value value of key this will make delete more certain (in case key name is same).

mixed Either true or null, if postmeta data is deleted then true else null.

Last updated