Wordpress: get_post_meta function WordPress
BitsCN.com
Wordpress can set custom fields to facilitate expansion. wordpress uses clever database table design to achieve this goal. posts tables store articles, pages, and attachments, the corresponding postmeta table is used to store custom fields. it uses the post_id, key, and value design to store the values of custom fields.
Get_post_meta function usage:
Get_post_meta ($ post_id, $ key, $ single );
This function has three basic parameters:
$ Post_id -- the ID of the retrieved data article. use $ post-> ID to obtain the article ID.
$ Key -- Name of the custom field to be retrieved
$ Single -- this is a Boolean value. if it is set to true, the value of the field is directly returned in the form of a string. for a custom field, you can enter multiple values. if it is set to false, returns an array to display these values.
This function is defined in post. php of wordpress:
function get_post_meta($post_id, $key = '', $single = false) { return get_metadata('post', $post_id, $key, $single);}
Here is an example of using the wp_cache_set, wp_cache_get and get_post_meta functions:
ID; $post_views = wp_cache_get($post_id,'views'); if($post_views === false){ $post_views = get_post_meta($post_id, "views",true); if(!$post_views) $post_views = 0; } $post_views = $post_views + 1; wp_cache_set($post_id,$post_views,'views'); if($post_views%10 == 0){ update_post_meta($post_id, 'views', $post_views); } echo $post_views;?>
Link: http://www.tantengvip.com/2013/12/wordpress-get_post_meta/