Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created April 9, 2018 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/bf2afae0a1819a7623421ce7de1f4dbe to your computer and use it in GitHub Desktop.
Save whoisryosuke/bf2afae0a1819a7623421ce7de1f4dbe to your computer and use it in GitHub Desktop.
WORDPRESS: Create new post with meta data
<?php
# Credits: https://wordpress.stackexchange.com/questions/106973/wp-insert-post-or-similar-for-custom-post-type
$post_id = wp_insert_post(array (
'post_type' => 'your_post_type',
'post_title' => $your_title,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed', // if you prefer
'ping_status' => 'closed', // if you prefer
));
if ($post_id) {
// insert post meta
add_post_meta($post_id, '_your_custom_1', $custom1);
add_post_meta($post_id, '_your_custom_2', $custom2);
add_post_meta($post_id, '_your_custom_3', $custom3);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment