wp-includes/post.php:2707Read a post's custom field with get_post_meta(); pass true as the third argument to get one value instead of an array of every value for the key. Omitting $key returns all meta for the post. A missing key yields an empty string (single) or an empty array, and non-serialized scalars are returned as strings.
Retrieves a post meta field for the given post ID.
$post_idint$keystringoptional''$singlebooloptionalfalsemixedEvery example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Passing true for $single returns the value itself rather than an array of every row.
$price = get_post_meta( 2, 'price', true );
if ( '' !== $price ) {
echo 'Price: ', esc_html( $price );
} else {
echo 'That post has no price set.';
}A missing key returns an empty string with $single true, and an empty array without it.
A key can hold many rows; omitting $single gives you all of them in the order they were stored.
delete_post_meta( 3, 'ticket_url' );
add_post_meta( 3, 'ticket_url', 'https://example.com/early-bird' );
add_post_meta( 3, 'ticket_url', 'https://example.com/general' );
$urls = get_post_meta( 3, 'ticket_url' );
print_r( $urls );add_post_meta() appends a row; update_post_meta() replaces one.
function get_post_meta( $post_id, $key = '', $single = false ) { return get_metadata( 'post', $post_id, $key, $single );}Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.