WordPress Write custom storage fields in the relevant PHP function parsing _php tips

Source: Internet
Author: User
Tags prev

WordPress custom field is the article meta information (meta information), the use of this function, you can expand the function of the article is to learn the WordPress plug-in development and in-depth development of the theme of the necessary knowledge to facilitate the article to store some additional custom content.

Custom fields are not only used by plug-in developers, but the features of WordPress's featured images, custom page templates, and so on are stored in the form of a custom field.

In the article editor interface of the "custom column" plate can be managed in addition to the first name is "_" field (No "custom column" plate in the upper right corner of the "Display Options" open), so many WordPress own custom fields can not be used in this management.
Storage Principle

The custom field is very flexible, first of all, how it is stored and why it is so flexible. Database, stores the Wp_posts form of the article, with only the default 20 items for storing the required article information.

The Wp_postmeta table that stores the custom fields has only four items, respectively, meta_id (the ID of the custom field), post_id (the ID of the article that the custom field belongs to), Meta_key (the name of the custom field), and Meta_ Value (custom field values), generally we only need to pay attention to Meta_key and meta_value two items.

Because of this, each article can have the same name but different values of the custom field, and can have unlimited custom fields, any plug-ins and themes can use custom fields to expand the article information.

The name of a custom field in an article can be duplicated.

Add Field

Custom fields can be edited in the article page, but this article is mainly about the development of things.

To add a custom field, you can use the Add_post_meta () function:

Add_post_meta ($post _id, $meta _key, $meta _value, $unique);

Parameters:

$post _id

(integer) (must) the ID of the article that added the custom field.

Default value: None

$meta _key

(string) (must be) the name of the custom field.

Default value: None

$meta _value

(mixed) (must) customize the value of the field.

Default value: None

$unique

(Boolean) (optional) If a field already has the same name is added repeatedly. True allow; False is not allowed.

Default value: False

Update fields

To update the value of a field, you can use the Update_post_meta () function:

Update_post_meta ($post _id, $meta _key, $meta _value, $prev _value);

Parameters:

$post _id

(integer) (must) update the ID of the article for the custom field.

Default value: None

$meta _key

(string) (must) the name of the custom field that is updated.

Default value: None

$meta _value

(mixed) (must) the value of the custom field that is updated.

Default value: None

$prev _value

(mixed) (optional) This parameter is only useful if you have more than one custom field with the same name in an article. If left blank, all fields with the same name are updated, otherwise the fields of the values with the same value for this parameter are updated.

Default value: Empty string

Get fields

Get fields can use the Get_post_meta () function:

Get_post_meta ($post _id, $key, $single);

Parameters:

$post _id

(integer) (required) to get the article ID of the field, if it is in a loop, you can set it by using get_the_id ().

Default value: None

$key

(optional) The name of the field to get.

Default value: None

$single

(Boolean) (optional) If true, returns a string, or False returns an array, and the values of the custom fields of the same key value are combined into an array of sequences in the order in which they were added.

Default value: False

Example

Below is a simple example of browsing statistics:

function Bing_statistics_visitors ($cache = False) {
  if (!is_singular ()) return;
  Global $post;
  $id = $post->id;
  if (Get_post ($id)->post_status!= ' publish ') return;
  $post _views = (int) Get_post_meta ($id, ' views ', true);
  Update_post_meta ($id, ' views ', ($post _views + 1)) | | Add_post_meta ($id, ' views ', 1, true);
}
Add_action (' Wp_head ', ' bing_statistics_visitors ');
Get Count:
function Bing_get_views () {
  global $post;
  $views = Number_format ((int) Get_post_meta ($post->id, ' views ', true));
  return $views;
}

Custom fields that are not visible
Although there are many custom fields created by WordPress in the database, but not shown in the "Custom column" of the article editor (there is no "custom column" in the upper right corner "display Options" open), such as featured images, Custom page templates and comments are open, and so on.

If you look closely, you will find that the names of these custom fields begin with the underscore "_", so that the custom fields beginning with the underscore "_" are not displayed to the user, and the The_meta () function will not output, which is hidden from the user.

With this feature, we can use fields that don't want to be modified by the user, such as setting options for the settings interface, caching data, and so on, to avoid errors.

Below is a small example:

Add_post_meta (get_the_id (), ' _time_diff ', Time ());

In addition, if the custom field stores content that is an array, even if the name is not underlined, "_" is not displayed.

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.