Adding custom fields to WordPress posts using add_post_meta

The add_post_meta function in WordPress is used to add a new custom field (also known as post meta) to a specific post. This function can be useful for storing additional information related to a post, such as author information, ratings, or custom settings.

By using add_post_meta, developers can easily extend the functionality of WordPress by adding custom fields to posts, which can then be used to display additional information on the front end or to customize the behavior of the website.

Parameters Accepted by WordPress add_post_meta Function

The add_post_meta function in WordPress accepts the following parameters:

  • $post_id (int, required): Post ID.
  • $meta_key (string, required): Metadata name.
  • $meta_value (mixed, required): Metadata value. Must be serializable if non-scalar.
  • $unique (bool, optional, default value: false): Whether the same key should not be added.

Value Returned by WordPress add_post_meta Function

The add_post_meta function in WordPress returns the following:

int|false: Meta ID on success, false on failure.

Examples

How to add a single post meta

$post_id = 123;
$meta_key = 'featured';
$meta_value = 'yes';
add_post_meta( $post_id, $meta_key, $meta_value, true );

This code snippet adds a single post meta with the key ‘featured’ and value ‘yes’ to the post with ID 123. The fourth parameter ‘true’ indicates that the meta key should be unique for the post.

How to add multiple post metas

$post_id = 456;
$meta_values = array( 'color' => 'blue', 'size' => 'large' );
foreach ( $meta_values as $meta_key => $meta_value ) {
 add_post_meta( $post_id, $meta_key, $meta_value, true );
}

This code snippet adds multiple post metas to the post with ID 456. It uses a foreach loop to iterate through an array of meta keys and values, and adds each one using the add_post_meta function. The fourth parameter ‘true’ indicates that the meta keys should be unique for the post.

How to update an existing post meta

$post_id = 789;
$meta_key = 'price';
$new_meta_value = 100;
$old_meta_value = get_post_meta( $post_id, $meta_key, true );
if ( $old_meta_value !== '' ) {
 update_post_meta( $post_id, $meta_key, $new_meta_value, $old_meta_value );
} else {
 add_post_meta( $post_id, $meta_key, $new_meta_value, true );
}

This code snippet updates an existing post meta with the key ‘price’ to a new value of 100 for the post with ID 789. It first checks if the meta key already exists using the get_post_meta function, and then uses the update_post_meta function to update the value if it exists, otherwise it adds a new meta using the add_post_meta function.

Conclusion

In conclusion, the add_post_meta function is an useful component for adding custom meta data to WordPress posts. It allows developers to easily store additional information about posts, such as author, date, and custom fields. By using this function, developers can enhance the functionality and flexibility of their WordPress websites. With proper usage and understanding of its parameters, the add_post_meta function can greatly improve the user experience and extend the capabilities of WordPress websites.

Related WordPress Functions