Post Meta Functions
OneMeta provides three helper functions for reading, writing, and deleting field values on posts, pages, and custom post types.
onemeta_get_meta()
Retrieve a field value for a post.
php
onemeta_get_meta( int $post_id, string $key, mixed $default = '' ): mixed
| Parameter | Type | Description |
|---|---|---|
$post_id | int | The post ID. Use get_the_ID() inside The Loop. |
$key | string | The field key, without the onemeta_ prefix. |
$default | mixed | Value to return if no value has been saved. Default: ''. |
Returns: The saved field value, or $default if the field has never been saved.
php
// Basic usage
$value = onemeta_get_meta( get_the_ID(), 'hero_title' );
// With a default fallback
$value = onemeta_get_meta( get_the_ID(), 'hero_title', 'Welcome' );
// Outside The Loop — pass a specific post ID
$value = onemeta_get_meta( 42, 'hero_title' );
onemeta_update_meta()
Save or update a field value for a post.
php
onemeta_update_meta( int $post_id, string $key, mixed $value ): bool|int
| Parameter | Type | Description |
|---|---|---|
$post_id | int | The post ID. |
$key | string | The field key, without the onemeta_ prefix. |
$value | mixed | The value to save. |
Returns: Meta ID on first insert, true on update, false on failure.
php
// Save a string value
onemeta_update_meta( get_the_ID(), 'hero_title', 'New Title' );
// Save an array (for checkbox, gallery, or repeater fields)
onemeta_update_meta( get_the_ID(), 'features', [ 'wifi', 'parking', 'gym' ] );
onemeta_delete_meta()
Delete a saved field value from a post.
php
onemeta_delete_meta( int $post_id, string $key ): bool
| Parameter | Type | Description |
|---|---|---|
$post_id | int | The post ID. |
$key | string | The field key, without the onemeta_ prefix. |
Returns: true on success, false on failure.
php
onemeta_delete_meta( get_the_ID(), 'hero_title' );
Important Notes
- Never include the
onemeta_prefix in your field key — it is added automatically by all three functions onemeta_get_meta()returns$defaultonly when the field has never been saved. If a field was saved as an empty string, the empty string is returned — not the default- These functions are wrappers around WordPress core
get_post_meta(),update_post_meta(), anddelete_post_meta()