Hongmu Notes
Home Program Notes WordPress `update_post_meta` function
Program Notes wordpress

WordPress `update_post_meta` function

WordPress `update_post_meta` function

update_post_meta() This is a WordPress function used to update custom fields for articles or pages; it is also known as "metadata." Its function prototype is as follows:

update_post_meta( int $post_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' )

Parameter description:

  • $post_idRequired: The ID of the article or page.
  • $meta_keyRequired. Name of the custom field.
  • $meta_valueRequired. Value for the custom field.
  • $prev_valueOptional. Used to check whether the specified value already exists. If specified, this condition is met only when the value of the custom field equals $prev_value It will only be updated when needed.

Call update_post_meta() The function can update the custom field data for a specific article or page. For example, if an article or page contains a custom field named "author," you can use the following code to update its value:

update_post_meta( get_the_ID(), 'author', 'John Doe' );

In the above code,get_the_ID() The first parameter is used to retrieve the ID of the current article or page; the second parameter is the name of the custom field; the third parameter is the value to be updated.

Please note that,update_post_meta() The function searches for custom fields based on the custom field name and the article/page ID; if a match is found, its value is updated; if no match is found, a new custom field is created.

If you need to update a custom field but must first check whether that field already exists and whether its current value equals a specific value, you can pass this information as the fourth parameter to the function. $prev_valueFor example, the following code updates the custom field "author" to "John Doe" only when its current value is "Jack":

update_post_meta( get_the_ID(), 'author', 'John Doe', 'Jack' );

If the custom field name does not exist, no changes will be made, and the function will return `false`. If the custom field value has not changed, the custom field will not be updated, and the function will return `false`. If the update is successful, the function will return `true`.

Please note that due to WordPress caching, if you update the value of a custom field in your code but the new value does not appear on the page, you may try using the following approach: wp_cache_flush() Clears the cache.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

What should I do if the Redis Object Cache plug-in cannot be linked after wordpress redis changes the password?

What should I do if the Redis Object Cache plug-in cannot be linked after wordpress redis changes the password? Program Notes wordpress

Problem description: My wordpress site server uses pagoda panel, installed php8.0, and installed redis on the extension panel, and installed Redis Object Cache plug-in in wordpress. In fact, in general, you don't need to do anything, just start it directly. However, it is unsafe for radis not to set a password, which is a risky loophole, so I still think it is better to set a password, …
👁 255
How do I disable revisions and automatic draft saving in WordPress?

How do I disable revisions and automatic draft saving in WordPress? Program Notes wordpress

WordPress's automatic article revision tracking feature logs every time you edit an article in the后台; each revision is recorded as a separate entry in the `wp_posts` table. Due to the interplay between article revisions and automatic saving, the article ID often grows larger over time. While this typically does not cause significant issues for your WordPress installation, an excessive number of article versions can place a considerable burden on your storage space and database...
👁 372
Detailed Tutorial on Migrating EmpireCMS Blog Data to WordPress

Detailed Tutorial on Migrating EmpireCMS Blog Data to WordPress Program Notes wordpress

PS1: Second revision – now supports data transfer between categories, posts, and tags. PS2: If the post IDs consistently don't match, you can try clearing the WordPress post category data table and relationship tables. I previously used Typecho for my blog, but later migrated to Empire; however, Empire has too many features – using it for a blog feels like putting a large hammer into a small hole! Additionally, I often record important code snippets in my blog – all of which I've painstakingly written...
👁 546
WordPress issue: pages freezing due to image transcoding

WordPress issue: pages freezing due to image transcoding Program Notes wordpress

Last night, while uploading an image, for some reason, the uploaded image was automatically transcoded. Transcoding type: BASE64 encoding. For an image on a website, simply upload it to your server and then access the link – the image will then be displayed. However, there is another approach: directly transcoding the image; then you can simply access the converted image using its encoded URL. Problem description: When an image is relatively small (e.g., only a few KB in size), transcoding it and then hosting it on a website can help avoid...
👁 152
WordPress database query operations

WordPress database query operations Program Notes wordpress

To connect to a database for a WordPress installation, you need to include the `wp-config.php` file in your PHP script. This file contains the database configuration details for WordPress installation and other constants. Here is a simple example demonstrating how to include the `wp-config.php` file and establish a database connection: // Include the wp-config.php file: require...
👁 377
WordPress MySQL Database Table and Subtable Structure Functionality Guide

WordPress MySQL Database Table and Subtable Structure Functionality Guide Program Notes wordpress

WordPress uses a MySQL database. Its data table structure differs slightly from that of conventional databases; data can be stored either in the form of standard data tables or as data elements – the latter approach is less intuitive when viewed directly. To become a developer, you must understand the basic structure of the WordPress database and be able to use it within your own plugins or themes to write to or read from the database. I. Data Tables – As of WordPress...
👁 374

Recommended reading

Responsive outdoor wedding photography website template 0464

Responsive outdoor wedding photography website template 0464 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the outdoor wedding photography industry. Its romantic and elegant design beautifully showcases outdoor wedding photos, photography services, travel destinations, and client testimonials, helping wedding photography agencies attract prospective couples online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 56
(Adaptive mobile version) Responsive electronic technology device PBootCMS website template – Download HTML5 electronic components-based website source code: 0772

(Adaptive mobile version) Responsive electronic technology device PBootCMS website template – Download HTML5 electronic components-based website source code: 0772 Practical Collection pbootcms Template

A responsive electronic technology device and electronic components-themed PbootCMS website template, compatible with both PC and WAP devices. Featuring a modern, tech-oriented design, this template is ideal for electronic component manufacturers or electronics brands to showcase their product lines. It helps electronics companies demonstrate their technical expertise online and attract customers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5...
👁 33
Responsive branded watch retail and recycling website template – 0465

Responsive branded watch retail and recycling website template – 0465 Practical Collection Yiyou template

An eYouCMS responsive website template designed for the branded watch retail and recycling industry. Its sophisticated and elegant design is ideal for showcasing watch products, brand stories, recycling services, and appraisal services. This template helps watch brands showcase their products online and attract watch enthusiasts and collectors. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About Installing eYouCMS...
👁 43
jquery selector

jquery selector Language Notes JavaScript

When the elements are at the same level, such as the following call, click the corresponding li tag, and the div tag corresponding to the same level of the li tag will display the HTML code: <ul id="open_list"> <li class="product_Level" data-fid="1"> <a>Cloud Server</a> &…
👁 158
(Adaptive mobile version) PBootCMS template for fashion women's clothing display websites; Download source code for women's clothing franchise websites – 0773

(Adaptive mobile version) PBootCMS template for fashion women's clothing display websites; Download source code for women's clothing franchise websites – 0773 Practical Collection pbootcms Template

A stylish women's clothing showcase and franchise PbootCMS website template, compatible with both PC and WAP devices. Featuring a modern, high-end design, this template is ideal for women's clothing brands to showcase new products, share brand stories, and present franchise policies – helping brands attract agents and consumers online. Template Demo | Installation Guide | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 46
Responsive potted plant and floral website template – 0466

Responsive potted plant and floral website template – 0466 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the greenery, flowers, and potted plants industry. Its fresh and natural design style is perfect for showcasing plant and floral products, potted plant designs, brand stories, and gardening services. It helps floral and greenery brands showcase their products online and attract both individual and corporate customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Installation Questions for EyouCMS...
👁 40