Wordpress obtains the custom field get_post_meta function _ MySQL

Source: Internet
Author: User
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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.