Skip to content

Instantly share code, notes, and snippets.

@someguy9
Last active February 14, 2023 17:46
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 someguy9/91f76edeae153001d775b5a66de35b39 to your computer and use it in GitHub Desktop.
Save someguy9/91f76edeae153001d775b5a66de35b39 to your computer and use it in GitHub Desktop.
Insert a custom post type programmatically in WordPress https://smartwp.com/wordpress-insert-post-programmatically/
<?php
// Insert custom post programmatically
$new_post = array(
'post_title' => 'My new example post',
'post_content' => 'My new content!',
'post_status' => 'public',
'post_type' => 'my_post_type'
);
$post_id = wp_insert_post( $new_post );
if( $post_id ){
// Update custom field on the new post
update_post_meta( $post_id, 'my_custom_field', 'Hello!' );
} else {
echo "Error, post not inserted";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment