WordPress Codesnippets

add post meta

				
					
				
			

value, $unique );

If you want to save post meta data when a post is saved, you need to register a callback function using the save_post action hook:
add_action( 'save_post', 'my_custom_post_meta' );

So the code you posted simply defines a function my_custom_post_meta() which uses add_post_meta() to add post meta data, and it registers that function as a callback for the save_post action hook.

Was this code snippet helpful?
YesNo

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmatically save post

This code creates a WordPress post with the values of the ‘post_title’ and ‘post_content’ variables. It sets the post status to ‘publish’ and the author

Read More »

Create order programmatically

This code creates a new post in the WordPress database with post type ‘shop_order’, post title ‘Order #12345’, post status ‘wc-processing’, and ping status ‘closed’.

Read More »