add post meta wordpress

To add post meta in WordPress, follow these steps:

  1. Identify the Post ID: You need to know the ID of the post for which you want to add meta data. You can find it by going to the WordPress dashboard and clicking on "Posts." Hover over the post you want and note the ID displayed in the URL.

  2. Use the add_post_meta Function: Use the add_post_meta function to add meta data to the identified post. The syntax is:

php add_post_meta( $post_id, $meta_key, $meta_value, $unique );

  • $post_id: Replace this with the ID of the post you want to add meta data to.
  • $meta_key: Specify the key or name of the meta data you want to add.
  • $meta_value: Provide the value for the meta data.
  • $unique (Optional): Set it to true if you want the meta data to be unique, preventing multiple entries with the same key and value combination.

  • Example Usage: Suppose you want to add a meta key "Author" with the value "John Doe" to a post with ID 123. Here's how you'd use the add_post_meta function:

php add_post_meta( 123, 'Author', 'John Doe', true );

This code snippet will add the "Author" meta key with the value "John Doe" to the post with ID 123.

  1. Verification: After adding the meta data, you can verify its addition by going to the post editor in WordPress and scrolling down to the "Custom Fields" or "Meta Box" section, depending on your WordPress version. There, you should see the added meta key and its value associated with the post.

Remember to replace placeholders like $post_id, $meta_key, and $meta_value with your actual values when implementing this in your WordPress code.