Home >Backend Development >PHP Tutorial >Analysis of related PHP functions for writing custom storage fields in WordPress, _PHP tutorial
The custom fields of WordPress are the meta information (meta information) of the article. Using this function, the article can be expanded The function is essential knowledge for learning WordPress plug-in development and theme in-depth development, and it is convenient for storing some additional customized content for articles.
Custom fields are not only used by plug-in developers. Information about WordPress’s featured images, custom page templates and other functions are stored in the form of custom fields.
The "Custom Column" section in the article editor interface can manage fields except for fields whose names begin with "_" (if there is no "Custom Column" section, please turn it on in the "Display Options" in the upper right corner), so many WordPress itself The custom fields used cannot be managed here.
Storage Principle
Custom fields are very flexible. First, let’s talk about its storage principle and why it is so flexible. In the database, the wp_posts form that stores articles has only the default 20 items, which are used to store necessary article information.
The wp_postmeta table that stores custom fields has only four items, namely meta_id (the ID of the custom field), post_id (the ID of the article to which the custom field belongs), meta_key (the name of the custom field) and meta_value (the value of a custom field), generally we only need to pay attention to the two items meta_key and meta_value.
Because of this, each article can have custom fields with the same name but different values, and there can be unlimited custom fields. Any plug-ins and themes can use custom fields to expand article information.
The name of a custom field in an article can be repeated.
Add field
Custom fields can be operated on the article editing page, but this article mainly writes about development.
To add custom fields, you can use the add_post_meta() function:
add_post_meta( $post_id, $meta_key, $meta_value, $unique );
Parameters:
$post_id
(integer) (required) The ID of the post to which the custom field is added.
Default value: None
$meta_key
(String) (Required) The name of the custom field.
Default value: None
$meta_value
(Mixed) (Required) The value of the custom field.
Default value: None
$unique
(Boolean) (optional) If there is already a field with the same name, whether to add it repeatedly. True allows; False disallows.
Default value: False
Update field
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) (required) The ID of the article to update the custom field.
Default value: None
$meta_key
(String) (Required) The name of the custom field to update.
Default value: None
$meta_value
(Mixed) (Required) The value of the custom field to update.
Default value: None
$prev_value
(Mixed) (Optional) This parameter is only useful if a post has multiple custom fields with the same name. If left blank, all fields with the same name are updated, otherwise fields with the same value as this parameter are updated.
Default value: empty string
Get field
To obtain fields, you can use the get_post_meta() function:
get_post_meta( $post_id, $key, $single );
Parameters:
$post_id
(integer) (required) The article ID of the field to be obtained, if in a loop, can be set using get_the_ID().
Default value: None
$key
(String) (optional) The name of the field to get.
Default value: None
$single
(Boolean) (optional) If True, returns a string; if False, returns an array, and the values of custom fields with the same key value will be combined into a sequence array in the order of addition.
Default value: False
Example
The following 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 the count:
function Bing_get_views(){ global $post; $views = number_format( (int) get_post_meta( $post->ID, 'views', true ) ); return $views; }
Invisible custom fields
Although there are many custom fields created by WordPress in the database, they are not displayed in the "Custom Column" of the article editor (there is no "Custom Column" section, please click the "Display Options" in the upper right corner enabled), such as whether featured images, custom page templates, and comments are enabled, etc.
If you observe carefully, you will find that the names of these custom fields all start with an underscore "_". Therefore, the custom fields starting with an underscore "_" will not be displayed to the user, and the_meta() function will not be displayed to the user. It is not output and is hidden from the user.
利用这个特性,我们可以把不想让用户随便修改的字段、会让用户感到困惑的字段,比如已经提供设置界面的设置选项、缓存数据等等,以免出现错误。
下边是一个小例子:
add_post_meta( get_the_ID(), '_time_diff', time() );
另外,如果自定义字段储存的内容是数组即使名字前边不是下划线 “_” 也不会显示。