get_post_meta() – Function description, parameters and basic usage


get_post_meta Returns the value of the selected Custom Field. It is very useful for advanced custom functionalities(like post thumbnails, sorting data by custom fields, adding additional info in posts etc.).


$post_id (required) -The id of the post which custom field you want the custom field from

$key (required) – The name(slug or meta value) of the custom field

$single (optional) – If you want to return a single result set it to true and the function will return a string of the needed data. If you set it to false(or don`t set it at all) it will return an array

Basic usage:

$custom_field = get_post_meta( $post->ID );

//$key and $single parameters are not set so it returns an associative array of all the custom fields

//The array structure will be similar to the following:


Example of usage of get_post_meta with $key and value set:

$meta = get_post_meta( $post->ID, “Test custom field”, true );
echo $meta;
//Returns a string with the meta_value. In this case “Test description”

One of the more advanced examples of using Custom Fields(and the most popular respectively) is creating post thumbnails. Create a custom field called thumb with value the link to your image.

About Pavel Petrov 2 Articles |  17 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.

Be the first to comment

Leave a Reply

Your email address will not be published.