I set up a custom content type called “entries” which is working fine. Now I’m inserting posts with that content type using wp_insert_post() and am encountering a problem adding a category. These are hierarchical categories that I associated with the post type and can set in the admin area, but I try to associate them using PHP code, it doesn’t work.
Here is a code example:
// Create post object $new_entry = array(); $new_entry['post_title'] = esc_html($_POST['submit-title']); $new_entry['post_content'] = wp_kses($_POST['submit-text'], $allowed_html); $new_entry['post_status'] = 'publish'; $new_entry['post_type'] = 'entries'; $new_entry['post_author'] = $userID; $new_entry['post_category'] = array(6,7); // this is the line that doesn't work // Insert the post into the database $entry_id = wp_insert_post( $new_entry );
This all works and the custom post is inserted and shows up as expected, but it doesn’t have any categories assigned to it. (Categories 6 and 7 are categories that are associated with the “entries” post type.)
I also tried using wp_set_post_categories after I created the new post with wp_insert_post but that didn’t work either. This is the code I used for that:
wp_set_post_categories( $entry_id, array(6,7) );
Does anyone have any thoughts on what I’m doing wrong or other approaches they recommend?
- The topic ‘Problem with wp_insert_post for custom content type’ is closed to new replies.