WordPress is a popular content management system that offers many functionalities to its users. One of such functionalities is the add_post_meta function. As the name suggests, the add_post_meta function is used to add metadata to a post.
Metadata is essentially data about data. It provides additional information about the post, such as the author, date, tags, categories, or any custom information that you want to add to the post. Using the add_post_meta function, you can add metadata to a post in WordPress.
The syntax of the add_post_meta function is as follows:
add_post_meta( $post_id, $meta_key, $meta_value, $unique );
Here’s what each parameter of the function means:
$post_id
(integer) (required): The ID of the post to which you want to add metadata.$meta_key
(string) (required): The key of the metadata that you want to add.$meta_value
(mixed) (required): The value of the metadata that you want to add.$unique
(boolean) (optional): If true, the metadata will only be added if it doesn’t already exist. If false, the metadata will always be added.
Here’s an example of how you can use the add_post_meta function to add metadata to a post:
$post_id = 123; // The ID of the post you want to add metadata to
$meta_key = 'author'; // The key of the metadata you want to add
$meta_value = 'John Doe'; // The value of the metadata you want to add
// Add the metadata to the post
add_post_meta( $post_id, $meta_key, $meta_value );
In this example, we’re adding the author metadata to a post with the ID of 123. The key of the metadata is ‘author’, and the value of the metadata is ‘John Doe’.