Skip to content

Post Meta

Use onemeta_get_meta(), onemeta_update_meta(), and onemeta_delete_meta() to work with post and page field values.

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
ParameterTypeDescription
$post_idintThe post ID. Use get_the_ID() inside The Loop.
$keystringThe field key, without the onemeta_ prefix.
$defaultmixedValue 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
ParameterTypeDescription
$post_idintThe post ID.
$keystringThe field key, without the onemeta_ prefix.
$valuemixedThe 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
ParameterTypeDescription
$post_idintThe post ID.
$keystringThe 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 $default only 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(), and delete_post_meta()

On This Page