// Include the necessary WordPress files require_once(ABSPATH . 'wp-includes/post.php'); // Create an array of post details for the new post $new_post = array( 'post_title' => 'Test Post with Metadata', // Parsed post data - Set the title of the new post 'post_content' => 'This is a custom post with metadata and taxonomy terms.', // Post content - Set the content of the new post 'post_type' => 'post', // Processed post data - Set the post type to 'post' 'post_status' => 'publish', // Post status - Set the status of the new post to 'publish' 'post_author' => 1, // Replace with the desired author's user ID 'post_date' => '2023-07-21 12:00:00', // Post date - Set a custom post date and time 'tags_input' => array('tag1', 'tag2', 'tag3'), // Processed attachment post data - Set tags for the new post 'page_template' => 'custom-template.php', // Invalid page template - Set a custom page template for the post ); // Insert WordPress post into the database $post_id = wp_insert_post($new_post); // Existing attachment post - Set the existing post ID for updating // Check if the post was successfully inserted if (!is_wp_error($post_id)) { // The post was successfully inserted, and $post_id contains the post ID // Get the post meta field based on the post ID $meta_value = get_post_meta($post_id, 'meta_key_name', true); // Add custom post categories (replace with actual category IDs) $post_categories = array(2, 4); // Filters attachment post data - Category IDs to assign to the post wp_set_post_categories($post_id, $post_categories); // Set post categories // Add custom post meta data update_post_meta($post_id, '_custom_meta_key', 'Custom Meta Value'); // Replace '_custom_meta_key' with your custom meta key and set its value echo "Post inserted successfully. New Post ID: " . $post_id; } else { // Error occurred while inserting the post echo "Error: " . $post_id->get_error_message(); }