Last active
February 14, 2023 17:46
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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